简体   繁体   中英

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. For this first set, I want my results to be 11/1/11 to 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.

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? Or throw me a bone as to what that 1969 is about? 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" ).

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

You are asking strtotime to interpret a rather nonsensical string like 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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