PHP Strtotime 不稳定函数
以下代码应获取二月的第四个星期五和四月的第四个星期五并返回它。
$datei1 = date(strtotime('fourth friday', strtotime('february', strtotime(date('01-m-Y')))));
$datei2 = date(strtotime('fourth friday', strtotime('april', strtotime(date('01-m-Y')))));
它有效,但需要四月的第五个星期五而不是第四个星期五。我只能假设它不相信四月一日。
任何想法,
太棒了
The following code should take the fourth friday of February and the fourth friday of April and return it.
$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. I can only assume that it does not believe the first of April counts.
Any ideas,
Marvellous
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码无缘无故地非常复杂。试试这个(PHP 5.3):
这将为您提供:
在 PHP 5.2 中,此语法似乎在所有情况下都有效:
Your code is very complicated for no reason. Try this (PHP 5.3):
This will give you:
In PHP 5.2, this syntax seems to work in all cases:
strtotime('fourth friday', $now)
可能真正的意思是“相对于$now
的第四个星期五”。您提供的
$now
参数是 2011 年 4 月 1 日,星期五。 4 月 1 日星期五之后的第四个星期五是该月的第五个星期五。strtotime('fourth friday', $now)
probably really means "the fourth Friday relative to$now
".The
$now
parameter you give is 1st April 2011, which is a Friday. The fourth Friday after Fri 1st April is the fifth Friday of the month.您似乎使用的是较旧版本的 PHP。为了处理您在这里遇到的边缘情况,我们进行了重大更改。在这种情况下,我认为以下是最干净的方法:
这应该为您提供今年四月的第四个星期五。我已在 5.3 中测试成功,但您必须在较旧的安装上进行测试。
It appears you're using an older version of 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:
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.
PHP 变更日志 ::
5.2.7 在 5.2.7 之前的 PHP 5 中,请求某个月份中某个给定工作日的给定出现(该工作日是该月的第一天)会错误地将一周添加到返回的时间戳中。这已在 5.2.7 及更高版本中得到纠正。
PHP Changelog ::
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. This has been corrected in 5.2.7 and later versions.