【问题标题】:PHP strtotime wrong only on sundaysPHP strtotime 仅在星期日出错
【发布时间】:2017-03-19 17:18:29
【问题描述】:

目前我正在使用以下获取本周和下周的星期一和星期五的日期。

$curWeekStart = date('d.m', strtotime('monday this week'));
$curWeekEnd = date('d.m', strtotime('friday this week'));

$nextWeekStart = date('d.m', strtotime('monday next week'));
$nextWeekEnd = date('d.m', strtotime('friday next week'));

在“星期天”到来之前一切正常,就像今天一样。那么上面的内容只显示星期一而不是其他日子。周二、周三、周四、周五 ecc 不起作用,它只在周一返回。此错误仅在星期日发生,其他日子不会发生。

这是输出:

$curWeekStart == 13.03;
$curWeekEnd == 13.03;

$nextWeekStart == 20.03;
$nextWeekEnd == 20.03;

有人知道如何解决这个问题吗?或者你知道这个的替代品吗?

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:

    这很可能是a bug 与affected PHP versions 5.6.23 to 5.6.30, 7.0.8 to 7.0.16, and 7.1.0 to 7.1.2 的结果。还有affects the DateTime class。

    该错误已在 PHP 7.0.17 和 7.1.3 中解决,但仍存在于最新版本的 PHP 5.6(撰写本文时为 5.6.30)中,now receives only security fixes 因此可能永远不会出现收到修复。

    将 PHP 升级到未受影响的版本可能是最好的解决方案,但如果您需要受影响版本的解决方法you should be able to add a DateInterval to a Datetime 并获得您想要的结果。

    【讨论】:

    • 谢谢。我不知道有这样的错误。我想我会把 PHP 升级到没有那个 bug 的版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2011-05-08
    • 2012-09-02
    • 2013-04-19
    • 2013-05-15
    相关资源
    最近更新 更多