PHP strtotime() “二月第一个星期一"如果 2 月 1 日是星期一,则返回第二个星期一
我正在开发一个计算假期的 PHP 函数:
I'm working on a PHP function which calculates holidays:
function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)
假期以数组中的时间戳形式返回.
The holidays are returned as timestamps in an array.
因为我必须计算像二月的第一个星期一这样的日期,所以我尝试了 strtotime("first monday february $year")
,我发现这对于 2010 年不起作用,因为 02/01/2010 是星期一 - 我改为 2 月 8 日.
Since I have to calculate dates like the first Monday of February, I tried strtotime("first monday february $year")
and I've discovered that this does not work for 2010, since 02/01/2010 is a Monday - I get February 8th instead.
这个bug其实是在变更日志中提到的:在 5.2.7 之前的 PHP 5 中,如果该工作日是该月的第一天,则请求在一个月内出现给定的工作日会错误地将一周添加到返回的时间戳.这已在 5.2.7 及更高版本中得到纠正.
This bug is actually mentioned in the change log: In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.
但我使用的是 PHP 5.3.8.为什么我会遇到此错误?
But I'm using PHP 5.3.8. Why am I experiencing this error?
推荐答案
看起来你只是少了一个of":
Looks like you are just missing an "of":
echo date('Y-m-d', strtotime('first monday of february 2010'));
将给出预期结果.有关各种输入格式,请参阅 PHP 相对日期手册.
will give the expected result. See the PHP Manual on Relative dates for the various input formats.
相关文章