[英]strtotime bug on thursday?
首先,我要执行的操作是获取特定日期后接下来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年即将来临一事不bone? 直到昨天,当我注意到星期四的结果没有显示在页面上时,所有内容似乎都可以正常工作,我对此进行了追溯。 谢谢你的时间。
我认为您需要在星期几之前留一个空格:
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");
我不知道为什么您在“星期四”和一周中的其他日子会得到不同的结果,但是无论如何插入空格还是有意义的( "2011-10-31 thursday"
与"2011-10-31thursday"
"2011-10-31 thursday"
"2011-10-31thursday"
) 。
嗯...你不想这样做吗?
$dateFrom = strtotime($date);
$nxtMon = strtotime('next monday', $dateFrom);
// ...
$nxtSun = strtotime('next sunday', $dateFrom);
从您所希望的最终结果之上,在我看来,这应该是您需要做的所有事情。 看起来您似乎有点使我复杂化,而且您可能还没有完全阅读strtotime()
的手册页 -请注意第二个参数...
您正在要求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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.