繁体   English   中英

PHP Strtotime不稳定功能

[英]PHP Strtotime erratic function

以下代码应采用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')))));

它的工作,但采取4月5日,而不是第四个星期五。 我只能假设它不相信4月1日是很重要的。

有任何想法吗,

奇妙

您的代码无缘无故非常复杂。 试试这个(PHP 5.3):

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

这将为您提供:

2011-04-22

在PHP 5.2中,此语法似乎在所有情况下均适用:

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

strtotime('fourth friday', $now)真实含义可能是“相对于$now的第四个星期五”。

您提供的$now参数是2011年4月1日,即星期五。 4月1日星期五之后的第四个星期五是该月的第五个星期五。

看来您使用的是旧版PHP。 引入了重大更改来处理您在此处遇到的边缘情况。 在这种情况下,我认为以下是最干净的方法:

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'))));

这应该给您当年四月的第四星期五。 我已经在5.3中成功测试,但是您必须在较旧的安装上进行测试。

PHP更新日志::

5.2.7在5.2.7之前的PHP 5中,如果某月中的某天是某月的第一天,则请求在该月的给定日期进行给定的出现会错误地将一个星期添加到返回的时间戳中。 在5.2.7和更高版本中,此问题已得到纠正。

暂无
暂无

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

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