Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtotime php function not behaving as expected

I'm trying to write a bit of php to determine the date (Y-m-d format) for the next occurrence of a given weekday.

date('Y-m-d', strtotime('next monday'));

is returning

2011-08-29

instead of

2011-08-22

on my server. Not sure why. My server is an EC2 instance in Amazon's US East-Coast data center and it's currently 2011-08-21 9:40:00.

EDIT 1 Also date('Y-m-d', strtotime('next sunday')); returns 2011-08-28 instead of today. I tried out this code on http://writecodeonline.com/php/ ... beginning to think that tester is inaccurate.

like image 540
Casey Flynn Avatar asked Jan 20 '26 17:01

Casey Flynn


1 Answers

Just because your server is set to a certain timezone doesn't necessarily mean PHP will be acting on the same timezone. It is already Monday, August 22, 2011 in Europe, which also includes UTC. If your PHP is acting on this timezone, "next Monday" would be Aug. 29.

You should double-check your timezone settings in PHP. You can either run date_default_timezone_get() to check what settings your installation is using, or try forcing your PHP script to use the timezone you expect with date_default_timezone_set("America/New_York"); and seeing if you get the result you expect.

I just tested this:

date_default_timezone_set("America/New York");
echo date('Y-m-d', strtotime('next monday')); // prints 2011-08-22
date_default_timezone_set("UTC");
echo date('Y-m-d', strtotime('next monday')); // prints 2011-08-29

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!