简体   繁体   English

如果 2 月 1 日是星期一,PHP strtotime()“二月第一个星期一”返回第二个星期一

[英]PHP strtotime() "first monday february" returns second monday if february 1st is a monday

I'm working on a PHP function which calculates holidays:我正在研究一个计算假期的 PHP 函数:

function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)

The holidays are returned as timestamps in an array.假期作为数组中的时间戳返回。

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.由于我必须计算像二月的第一个星期一这样的日期,我尝试了strtotime("first monday february $year")并且我发现这不适用于 2010 年,因为 02/01/2010 是星期一 - 我得到2 月 8 日。

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.这个错误实际上是在更改日志中提到的:在 PHP 5 5.2.7 之前,请求一个给定的工作日发生在一个月的第一天,而该工作日是该月的第一天会错误地将一周添加到返回的时间戳中。 This has been corrected in 5.2.7 and later versions.这已在 5.2.7 及更高版本中得到纠正。

But I'm using PHP 5.3.8.但我使用的是 PHP 5.3.8。 Why am I experiencing this error?为什么我会遇到这个错误?

Looks like you are just missing an "of":看起来您只是缺少一个“of”:

echo date('Y-m-d', strtotime('first monday of february 2010'));

will give the expected result .将给出预期的结果 See the PHP Manual on Relative dates for the various input formats.请参阅有关各种输入格式的相对日期PHP 手册

Depending on your version of PHP the 'of' statement may or may not work.根据您的 PHP 版本,'of' 语句可能有效,也可能无效。 As another solution try:作为另一种解决方案,请尝试:

echo date('Ym-d',strtotime('monday February 2010'));

will return the first monday of February 2010. Works for all days as well.将在 2010 年 2 月的第一个星期一返回。也适用于所有日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM