简体   繁体   English

星期四的strtotime错误?

[英]strtotime bug on thursday?

First of all, what I am trying to do is get the dates of the next 7 days after a specific date. 首先,我要执行的操作是获取特定日期后接下来7天的日期。 For this first set, I want my results to be 11/1/11 to 11/7/11. 对于第一组,我希望我的结果是11/1/11至11/7/11。 Here is the code, and the result that it gives me. 这是代码,以及它给我的结果。 This is very, very strange. 这是非常非常奇怪的。

$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");

And it gives me: 它给了我:

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

So then I change it to this: 因此,我将其更改为:

$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");

And it gives me: 它给了我:

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!

Now I try it with the date equal to today's date: 现在,我尝试用等于今天的日期作为日期:

$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");

And I Get: 我得到:

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!

I seem to have figured out how to get exactly what I want, but the truth of the matter is that until I typed out this question it didn't seem to be working right. 我似乎已经想出了如何准确地得到自己想要的东西,但是事实是,在我输入这个问题之前,它似乎工作不正常。 Now suddenly it does. 现在突然如此。 It seems to work sometimes and not other times without changing anything. 它似乎有时可以工作,而不更改其他时间而不进行任何更改。 With the bizarreness of that 1969 in the first result, I'm feeling like I have no idea what is going on. 第一个结果是1969年的奇异之处,我感觉好像不知道发生了什么。

Does this look like a sound way to do what I am trying to do? 这看起来像是完成我要尝试的声音的好方法吗?

If anyone knows a better way to input a date and get the next 7 dates, could you help me out? 如果有人知道输入日期并获取下7个日期的更好方法,您能帮我吗? Or throw me a bone as to what that 1969 is about? 还是让我对1969年即将来临一事不bone? Everything seemed to be working fine until yesterday when I noticed Thursday results weren't showing up on my page and I traced it to this. 直到昨天,当我注意到星期四的结果没有显示在页面上时,所有内容似乎都可以正常工作,我对此进行了追溯。 Thanks for your time. 谢谢你的时间。

I think you need a space before the day of the week: 我认为您需要在星期几之前留一个空格:

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

I don't know why you're getting different results for "thursday" vs. other days of the week, but inserting a space makes sense anyway ( "2011-10-31 thursday" vs. "2011-10-31thursday" ). 我不知道为什么您在“星期四”和一周中的其他日子会得到不同的结果,但是无论如何插入空格还是有意义的( "2011-10-31 thursday""2011-10-31thursday" "2011-10-31 thursday" "2011-10-31thursday" ) 。

Erm... don't you want to be doing this...? 嗯...你不想这样做吗?

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

From what you describe as wanting the end result to be above, it seems to me like this should be all you need to do. 从您所希望的最终结果之上,在我看来,这应该是您需要做的所有事情。 Looks like you are somewhat over complicating it to me, plus you may not have read the manual page for strtotime() fully - note the second argument... 看起来您似乎有点使我复杂化,而且您可能还没有完全阅读strtotime()手册页 -请注意第二个参数...

You are asking strtotime to interpret a rather nonsensical string like 2011-10-11next monday . 您正在要求strtotime解释一个像2011-10-11next monday一那样荒唐的字符串。 As you see this works in some cases and doesn't in others. 如您所见,这在某些情况下有效,而在其他情况下则无效。 I wouldn't rely on it. 我不会依赖它。 This is more robust: 这更可靠:

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

This turns $date into a timestamp and requests the "next monday" relative to that timestamp. 这会将$date转换为时间戳,并请求相对于该时间戳的“下一个星期一”。 This should work a lot better. 这应该工作得更好。 Alternatively, do a little manual calculation: 或者,进行一些手动计算:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM