php函数获取日期范围内的所有星期一 [英] php function for get all mondays within date range

查看:587
本文介绍了php函数获取日期范围内的所有星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例: $startDate是2007年2月5日星期一,而$endDate是2007年2月20日星期二.然后我要列出:

Example: $startDate is Monday 2007-02-05 and $endDate is Tuesday 2007-02-20. Then I want it to list:

Monday 2007-02-05
Monday 2007-02-12
Monday 2007-02-19

我查看了PHP手册,发现它可以获取两个日期之间的所有日期.但是如何按照我想要的方式来做呢? PHP代码:

I looked at the PHP manual and found this to get all the days between two dates. But how to do it the way i want? PHP Code:

推荐答案

与其获取所有日期并遍历所有日期,不如获取开始日期之后的第一个星期一,然后一次迭代7天:

Rather than get all days and loop through them all, get the first Monday after the start date and then iterate 7 days at a time:

$endDate = strtotime($endDate);
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
    echo date('l Y-m-d', $i);

这篇关于php函数获取日期范围内的所有星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆