创建工作日数组
我需要一种在 PHP 中创建 2 个日期之间的“工作日”数组的方法。
例如:
getWorkingDays("2008-01-01","2009-06-30");
将创建一个如下所示的数组:
Array
(
[0] ="2008-01-01",
[1] ="2008-01-05",
[2] ="2008-01-06",
[3] ="2008-01-07",
[4] ="2008-01-08",
[5] ="2008-01-09",
[6] ="2008-01-12",
[7] ="2008-01-13",
[8] ="2008-01-14",
...
)
I need a method for creating an array of "business days" in PHP beteewn 2 dates.
For example:
getWorkingDays("2008-01-01","2009-06-30");
Will create an array like the following:
Array
(
[0] ="2008-01-01",
[1] ="2008-01-05",
[2] ="2008-01-06",
[3] ="2008-01-07",
[4] ="2008-01-08",
[5] ="2008-01-09",
[6] ="2008-01-12",
[7] ="2008-01-13",
[8] ="2008-01-14",
...
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的技术是创建一个从开始日期到结束日期的循环(您应该通过 mktime,然后添加一天(86400 秒)作为增量)。在循环的内部,您将每个日期转换为 unix 时间戳(再次通过 mktime 等),然后使用...
...获取相关日期是星期几。
作为基本实现,这看起来像:
但是,您很可能还需要省略银行假日等,因此您应该将此类异常存储在数据库表/查找数组等中,如下所示酌情检查它们。
顺便说一句,如果您使用的 PHP 版本早于 5.1.0,则应使用
date('w'...
并相应地调整星期几。(它从零开始计数,而不是从零开始计数)一。)The simplest technique would be to create a loop from the starting date until the end date (you should covert both to timestamps via mktime and then add a day (86400 seconds) to increment). In the inner part of the loop, you'd convert each date into a unix timestamp (once again via mktime, etc.) and then use...
...to get the day of the week for the date in question.
As a basic implemention, this would look like:
However, you'll most likely also need to omit bank holidays, etc. so you should store such exceptions in a database table/lookup array, etc. as check against them as appropriate.
Incidentally, if you're using a version of PHP earlier than 5.1.0, you should use
date('w'...
and adjust your days of week accordingly. (It counts from zero instead of one.)有了基础的 php 日期函数,你就可以做到这一点,花点时间阅读文档 PHP 日期
With the basics php date function you can make it, take the time to read the documentation at PHP date