简体   繁体   English

PHP Strtotime不稳定功能

[英]PHP Strtotime erratic function

The following code should take the fourth friday of February and the fourth friday of April and return it. 以下代码应采用2月的第四个星期五和4月的第四个星期五并返回它。

$datei1 = date(strtotime('fourth friday', strtotime('february', strtotime(date('01-m-Y')))));

$datei2 = date(strtotime('fourth friday', strtotime('april', strtotime(date('01-m-Y')))));

Its working but taking the 5th friday of April not the fourth. 它的工作,但采取4月5日,而不是第四个星期五。 I can only assume that it does not believe the first of April counts. 我只能假设它不相信4月1日是很重要的。

Any ideas, 有任何想法吗,

Marvellous 奇妙

Your code is very complicated for no reason. 您的代码无缘无故非常复杂。 Try this (PHP 5.3): 试试这个(PHP 5.3):

$date = date('Y-m-d', strtotime('fourth friday of april 2011'));

This will give you: 这将为您提供:

2011-04-22

In PHP 5.2, this syntax seems to work in all cases: 在PHP 5.2中,此语法似乎在所有情况下均适用:

$date = date('Y-m-d', strtotime('fourth friday', strtotime('april - 1 second')));

strtotime('fourth friday', $now) probably really means "the fourth Friday relative to $now ". strtotime('fourth friday', $now)真实含义可能是“相对于$now的第四个星期五”。

The $now parameter you give is 1st April 2011, which is a Friday. 您提供的$now参数是2011年4月1日,即星期五。 The fourth Friday after Fri 1st April is the fifth Friday of the month. 4月1日星期五之后的第四个星期五是该月的第五个星期五。

It appears you're using an older version of PHP. 看来您使用的是旧版PHP。 Major changes have been introduced to handle the edge case you're experiencing here. 引入了重大更改来处理您在此处遇到的边缘情况。 In which case I believe the following is the cleanest approach: 在这种情况下,我认为以下是最干净的方法:

echo date('Y-m-d', strtotime('+4 fridays', mktime(0,0,0,4,0,date('Y'))));
// or
echo date('Y-m-d', strtotime('fourth friday', mktime(0,0,0,4,0,date('Y'))));

This should give you the fourth friday in april of the current year. 这应该给您当年四月的第四星期五。 I've tested successfully in 5.3 but you'll have to test on you older installation. 我已经在5.3中成功测试,但是您必须在较旧的安装上进行测试。

PHP Changelog :: PHP更新日志::

5.2.7 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. 5.2.7在5.2.7之前的PHP 5中,如果某月中的某天是某月的第一天,则请求在该月的给定日期进行给定的出现会错误地将一个星期添加到返回的时间戳中。 This has been corrected in 5.2.7 and later versions. 在5.2.7和更高版本中,此问题已得到纠正。

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

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