首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >strtotime bug在周四?

strtotime bug在周四?
EN

Stack Overflow用户
提问于 2011-10-23 07:06:06
回答 3查看 499关注 0票数 2

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

代码语言:javascript
复制
$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");

它给了我:

代码语言:javascript
复制
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

因此,我将其更改为:

代码语言:javascript
复制
$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");

它给了我:

代码语言:javascript
复制
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!

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

代码语言:javascript
复制
$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");

我得到了:

代码语言:javascript
复制
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年是怎么回事?直到昨天,当我注意到周四的结果没有显示在我的页面上时,一切似乎都很好,我追踪到了这一点。耽误您时间,实在对不起。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-23 07:18:43

我想你需要在星期几前留个空位:

代码语言:javascript
复制
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");

我不知道为什么“星期四”与一周中的其他几天会得到不同的结果,但不管怎样,插入一个空格是有意义的("2011-10-31 thursday""2011-10-31thursday")。

票数 3
EN

Stack Overflow用户

发布于 2011-10-23 07:19:42

呃..。你不想这么做吗...?

代码语言:javascript
复制
$dateFrom = strtotime($date);
$nxtMon = strtotime('next monday', $dateFrom);
// ...
$nxtSun = strtotime('next sunday', $dateFrom);

从你所描述的希望最终结果在上面,在我看来这应该是你所需要做的一切。看起来你有点把它复杂化了,再加上你可能还没有完全读完manual page for strtotime() --注意第二个论点……

票数 2
EN

Stack Overflow用户

发布于 2011-10-23 07:20:59

您是在要求strtotime解释一个像2011-10-11next monday这样相当无意义的字符串。正如您所看到的,这在某些情况下有效,而在其他情况下不起作用。我不会依赖它的。这个更健壮:

代码语言:javascript
复制
strtotime('next monday', strtotime($date))

这会将$date转换为时间戳,并请求与该时间戳相关的“下周一”。这应该会工作得更好。或者,执行一些手动计算:

代码语言:javascript
复制
$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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7863208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档