【问题标题】:strtotime bug on thursday?星期四的strtotime错误?
【发布时间】:2011-10-22 23:06:05
【问题描述】:

首先,我要做的是获取特定日期后接下来 7 天的日期。对于第一组,我希望我的结果是 11/1/11 到 11/7/11。这是代码,以及它给我的结果。这非常非常奇怪。

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "saturday");  
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "sunday");

它给了我:

2011-10-31    <---    I Want the Next Monday
2011-11-01
2011-11-02
1969-12-31    <---    ???????
2011-11-04
2011-11-05
2011-11-06

然后我把它改成这样:

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

它给了我:

2011-11-07    <---  Wow, it works!
2011-11-01    <---  Wow, it works!
2011-11-02    <---  Wow, it works!
2011-11-03    <---  Wow, it works!
2011-11-04    <---  Wow, it works!
2011-11-05    <---  Wow, it works!
2011-11-06    <---  Wow, it works!

现在我尝试使用与今天相同的日期:

$date = "October 22, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

我明白了:

2011-10-24
2011-10-25
2011-10-26
2011-10-27
2011-10-28
2011-10-29
2011-10-23   Hooray, it still works!

我似乎已经弄清楚了如何得到我想要的东西,但事实是,在我输入这个问题之前,它似乎并没有正常工作。现在突然就这样了。在不改变任何东西的情况下,它似乎有时有效,有时无效。由于第一个结果中 1969 年的怪异,我感觉我不知道发生了什么。

这看起来像我想做的事情的好方法吗?

如果有人知道输入日期并获取接下来 7 个日期的更好方法,您能帮帮我吗?或者告诉我 1969 年是关于什么的?直到昨天,当我注意到星期四的结果没有出现在我的页面上时,一切似乎都运行良好,我追查到了这一点。感谢您的宝贵时间。

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:

    我认为您在星期几之前需要一个空格:

    $nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");
    

    我不知道为什么“星期四”与一周中的其他日子会得到不同的结果,但无论如何插入空格都是有意义的("2011-10-31 thursday" 与 "2011-10-31thursday")。

    【讨论】:

    • 你一针见血。添加了空格,它就不再那么奇怪了。
    • 我不熟悉 php,但strtotime() 接受这么多无意义的字符串可能是一个错误。如果"2011-10-31wednesday" 也失败了,您可能会更快地找到错误。
    【解决方案2】:

    呃……你不想这样做吗……?

    $dateFrom = strtotime($date);
    $nxtMon = strtotime('next monday', $dateFrom);
    // ...
    $nxtSun = strtotime('next sunday', $dateFrom);
    

    根据您所描述的希望最终结果高于上述内容,在我看来,这应该是您需要做的所有事情。看起来你对我来说有点过于复杂了,而且你可能没有完全阅读manual page for strtotime() - 请注意第二个论点......

    【讨论】:

      【解决方案3】:

      您要求strtotime 解释一个相当荒谬的字符串,例如2011-10-11next monday。如您所见,这在某些情况下有效,而在其他情况下则无效。我不会依赖它。这更健壮:

      strtotime('next monday', strtotime($date))
      

      这会将$date 转换为时间戳并请求相对于该时间戳的“下周一”。这应该会更好。或者,做一些手动计算:

      $date  = 'October 31, 2011';
      $ts    = strtotime($date);
      $day   = date('j', $ts);
      $month = date('n', $ts);
      $year  = date('Y', $ts);
      
      for ($i = 0; $i < 7; $i++) {
          echo date('Y-m-d', mktime(0, 0, 0, $month, $day + $i, $year)) . PHP_EOL;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-23
        • 1970-01-01
        • 1970-01-01
        • 2011-05-08
        • 1970-01-01
        相关资源
        最近更新 更多