【发布时间】:2011-08-31 23:11:07
【问题描述】:
从以下格式的日期开始:2011-05-01 09:00:00,如何创建一个包含一年中所有工作日的所有办公时间(09:00 到 17:00)的数组(因此不包括所有周六和周日) .我想要到达的是这样的:
2011-05-01 09:00:00
2011-05-01 10:00:00
2011-05-01 11:00:00
2011-05-01 12:00:00
2011-05-01 13:00:00
2011-05-01 14:00:00
2011-05-01 15:00:00
2011-05-01 16:00:00
2011-05-01 17:00:00
//next day, starting at 09:00 and ending at 17:00
2011-05-02 09:00:00
...
2011-05-02 17:00:00
//until the last day of the year from 09:00 to 17:00
2011-12-31 09:00:00
...
2011-12-31 17:00:00
开始日期将是当月的第一天,时间为 09:00,最后一个日期(数组的最后一个元素)将始终是一年中最后一天的 17:00。
同样,应排除周末。
伪代码思路:
我想到了strtotime($start, "+1 one hour") 之类的东西,并检查了"if smaller than 17:00",但似乎没那么简单。
【问题讨论】:
-
如果您不关心周六/周日,只需将 86400 秒(1 天)添加到任何开始时间即可进行迭代。从一整天开始,然后迭代直到达到时间范围。然后跳过它,直到它再次正常。
标签: php datetime date-arithmetic