PHP Strtotime 不稳定函数

发布于 2024-10-29 06:51:49 字数 325 浏览 2 评论 0原文

以下代码应获取二月的第四个星期五和四月的第四个星期五并返回它。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听不够的曲调 2024-11-05 06:51:49

您的代码无缘无故地非常复杂。试试这个(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')));

Your code is very complicated for no reason. Try this (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:

$date = date('Y-m-d', strtotime('fourth friday', strtotime('april - 1 second')));
终止放荡 2024-11-05 06:51:49

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.

心如狂蝶 2024-11-05 06:51:49

您似乎使用的是较旧版本的 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 中测试成功,但您必须在较旧的安装上进行测试。

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:

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.

迷路的信 2024-11-05 06:51:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文