php函数获取日期范围内的所有星期一
例子: $startDate
是 2007-02-05 星期一,$endDate
是 2007-02-20 星期二。然后我希望它列出:
Monday 2007-02-05
Monday 2007-02-12
Monday 2007-02-19
我查看了 PHP 手册,发现这个可以获取两个日期之间的所有天数。但如何按照我想要的方式去做呢? PHP代码:
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
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不要获取所有日期并循环遍历所有日期,而是获取开始日期后的第一个星期一,然后一次迭代 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:
我需要同样的东西并创建了一个简单的方法。
然后使用它。
结果:
也许这对某人有帮助。
I needed the same and created a simple method.
Then use it.
Result:
Maybe it will be helpful for somebody.
您可以使用下面的函数来获取特定日期的日期范围之间的日期数组。
您必须在数字中输入开始日期、结束日期和天数。天数如下。
1 = 星期一,2 = 星期二,3 = 星期三,4 = 星期四。 5 = 星期五,6 = 星期六,7 = 星期日。
You can use below function to get a array of dates between a date range of specific day.
You have to input start date, end date and day number in number.The day number is as follow.
1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday. 5 = Friday, 6 = Saturday, 7 = Sunday.
我创建一个类,您将获得按日期名称排列的日期组范围内的所有日期:
输出将如下所示
i Create A class, You get All Days In range Date Group By Name of Day:
The Output will be like this
我对响应进行了一些更改 https://stackoverflow.com/a/37300272/6871295
然后我可以获得之间的天数任意一天的日期和返回格式。
I made some changes to response https://stackoverflow.com/a/37300272/6871295
Then I can get the days between dates for any day and return format.
这是用于获取“$startdate”的工作日并计算两个日期之间的工作日数的代码。
This is code for fetching the weekday of "$startdate" and counting the number of weekdays between two dates.
我有类似的问题,课程可以在任何一天开始。该脚本选择开始日期并每周收集接下来的几天,直到达到所需的数量(在本例中为 12)。
I had similar issue and courses can start on any day. This script picks starting day and collect next days every week until the wanted amount (12 in this case).
将之前的 $startDate 和 $endDate 转换为时间戳:
Convert $startDate and $endDate before that to timestamps:
只需添加为,
simply you can add as,