strtotime not producing expected results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rednarb
Forum Newbie
Posts: 4
Joined: Tue Feb 05, 2008 5:09 pm

strtotime not producing expected results

Post by Rednarb »

I'm trying to build a quick event calender for upcoming events. So ideally I'd want to see all events happening this week and next week. I did some research and strtotime seemed ideal! However, its not giving me results I'd expect (or desire). For instance, I want all the events scheduled for THIS WEEK. But since today is Tuesday, strtotime assumes that "monday" and "this monday" means next week! Is this right?

Code: Select all

echo date("D Y-m-d",strtotime("last monday"));
echo "<br>";
echo date("D Y-m-d",strtotime("monday"));
echo "<br>";
echo date("D Y-m-d",strtotime("next monday"));
Results in

Code: Select all

Mon 2008-02-04
Mon 2008-02-11
Mon 2008-02-11
I would hope that "monday" would come up with 2008-02-04. BTW, using "this monday" produces the same results as using "monday".

If this is correct behavior then I guess I need to do some conditionals to take into account adding "last" or not... can you offer any suggestions for that?

BTW, here are my specs:
PHP Version 5.2.3-1ubuntu6.3
Linux jii-btn1 2.6.22-14-server #1 SMP Tue Dec 18 08:31:40 UTC 2007 i686
Apache/2.2.4 (Ubuntu) PHP/5.2.3-1ubuntu6.3 mod_ssl/2.2.4 OpenSSL/0.9.8e

TIA,

Eric
Rednarb
Forum Newbie
Posts: 4
Joined: Tue Feb 05, 2008 5:09 pm

Re: strtotime not producing expected results

Post by Rednarb »

I did some tinkering and came up with this code that should do what I want:

Code: Select all

$week1['monday'] = (date(N) > 1) ? date("Y-m-d",strtotime("last monday")) : date("Y-m-d",strtotime("monday"));
$week1['tuesday'] = (date(N) > 2) ? date("Y-m-d",strtotime("last tuesday")) : date("Y-m-d",strtotime("tuesday"));
$week1['wednesday'] = (date(N) > 3) ? date("Y-m-d",strtotime("last wednesday")) : date("Y-m-d",strtotime("wednesday"));
$week1['thursday'] = (date(N) > 4) ? date("Y-m-d",strtotime("last thursday")) : date("Y-m-d",strtotime("thursday"));
$week1['friday'] = (date(N) > 5) ? date("Y-m-d",strtotime("last friday")) : date("Y-m-d",strtotime("friday"));
$week1['saturday'] = (date(N) > 6) ? date("Y-m-d",strtotime("last saturday")) : date("Y-m-d",strtotime("saturday"));
$week1['sunday'] = date("Y-m-d",strtotime("sunday"));
 
$week2['next_monday'] = date("Y-m-d",strtotime("next monday"));
$week2['next_tuesday'] = date("Y-m-d",strtotime("next tuesday"));
$week2['next_wednesday'] = date("Y-m-d",strtotime("next wednesday"));
$week2['next_thursday'] = date("Y-m-d",strtotime("next thursday"));
$week2['next_friday'] = date("Y-m-d",strtotime("next friday"));
$week2['next_saturday'] = date("Y-m-d",strtotime("next saturday"));
$week2['next_sunday'] = date("Y-m-d",strtotime("next sunday"));
echo "<pre>";
print_r($week1);
echo "</pre>";
echo "<pre>";
print_r($week2);
echo "</pre>";
Outputs:

Code: Select all

Array
(
    [monday] => 2008-02-04
    [tuesday] => 2008-02-05
    [wednesday] => 2008-02-06
    [thursday] => 2008-02-07
    [friday] => 2008-02-08
    [saturday] => 2008-02-09
    [sunday] => 2008-02-10
)
 
Array
(
    [next_monday] => 2008-02-11
    [next_tuesday] => 2008-02-12
    [next_wednesday] => 2008-02-06
    [next_thursday] => 2008-02-07
    [next_friday] => 2008-02-08
    [next_saturday] => 2008-02-09
    [next_sunday] => 2008-02-10
)
 
How does that look? Is that a good way to skin this cat?

TIA,

Eric
Rednarb
Forum Newbie
Posts: 4
Joined: Tue Feb 05, 2008 5:09 pm

Re: strtotime not producing expected results

Post by Rednarb »

Dag nabbit I didn't catch that half of next week is messed up. There must be a better way! I don't want to reinvent the wheel here, can anyone lend me a suggestion?

TIA,

Eric
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: strtotime not producing expected results

Post by Zoxive »

Rednarb wrote:Dag nabbit I didn't catch that half of next week is messed up. There must be a better way! I don't want to reinvent the wheel here, can anyone lend me a suggestion?

TIA,

Eric
What are you trying to accomplish? I would suggest stop using strtotime(), maybe use mktime();
Rednarb
Forum Newbie
Posts: 4
Joined: Tue Feb 05, 2008 5:09 pm

Re: strtotime not producing expected results

Post by Rednarb »

I think I found a solution. May I present My New Wheel:

Code: Select all

$week1['monday'] = (date(N) > 1) ? date("Y-m-d",strtotime("last monday")) : date("Y-m-d",strtotime("monday"));
$week1['tuesday'] = date("Y-m-d",strtotime($week1['monday']."+ 1 day"));
$week1['wednesday'] = date("Y-m-d",strtotime($week1['monday']."+ 2 day"));
$week1['thursday'] = date("Y-m-d",strtotime($week1['monday']."+ 3 day"));
$week1['friday'] = date("Y-m-d",strtotime($week1['monday']."+ 4 day"));
$week1['saturday'] = date("Y-m-d",strtotime($week1['monday']."+ 5 day"));
$week1['sunday'] = date("Y-m-d",strtotime($week1['monday']."+ 6 day"));
 
$week2['next_monday'] = date("Y-m-d",strtotime($week1['monday']."+ 7 day"));
$week2['next_tuesday'] = date("Y-m-d",strtotime($week1['monday']."+ 8 day"));
$week2['next_wednesday'] = date("Y-m-d",strtotime($week1['monday']."+ 9 day"));
$week2['next_thursday'] = date("Y-m-d",strtotime($week1['monday']."+ 10 day"));
$week2['next_friday'] = date("Y-m-d",strtotime($week1['monday']."+ 11 day"));
$week2['next_saturday'] = date("Y-m-d",strtotime($week1['monday']."+ 12 day"));
$week2['next_sunday'] = date("Y-m-d",strtotime($week1['monday']."+ 13 day"));
echo "<pre>";
print_r($week1);
echo "</pre>";
echo "<pre>";
print_r($week2);
echo "</pre>";
Which comes up with:

Code: Select all

Array
(
    [monday] => 2008-02-04
    [tuesday] => 2008-02-05
    [wednesday] => 2008-02-06
    [thursday] => 2008-02-07
    [friday] => 2008-02-08
    [saturday] => 2008-02-09
    [sunday] => 2008-02-10
)
 
Array
(
    [next_monday] => 2008-02-11
    [next_tuesday] => 2008-02-12
    [next_wednesday] => 2008-02-13
    [next_thursday] => 2008-02-14
    [next_friday] => 2008-02-15
    [next_saturday] => 2008-02-16
    [next_sunday] => 2008-02-17
)
I bet there is still a better way but until I find it I guess it will work. Should I flag this one as solved?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: strtotime not producing expected results

Post by Kieran Huggins »

"next Tuesday" in general usage means next week's Tuesday, whereas strtotime() interprets it as "the next Tuesday after now". Once you get your head around that it's all good.
Post Reply