I’ve been trying to understand and debug timezones between MySQL and PHP.
The dates are being created in MySQL using NOW() in a datetime field. The initial problem I was trying to figure out is what timezone MySQL is using and how to sync it up with PHP. But now there’s an even weirder issue.
In my test app, if you change your timezone, the first strtotime updates properly, but if you pass it into a date() function it doesn’t change. This is how I’m setting the timezone based off the select box.
$current_timezone = 'America/New_York';
if( isset( $_GET['timezone'] ) ) $current_timezone = $_GET['timezone'];
date_default_timezone_set($current_timezone);
Any ideas as to why the date() function isn’t accepting the timezone changes?
The date() function is operating correctly.
the default timezone also affects strtotime.
Notice that when you change the timezone, the only column in your table that changes is the timestamp column.
You need to determine (or set) the timezone that mysql is operating in. I would recommend UTC.
Then try one of the following:
either
date_default_timezone_set(‘UTC’)
before calling strtotime(), and then
set it back to the user preference
before calling date
Try adding a timezone part to the date string you get back from mysql:
//since we added a timezone part, this will be 20:14:01 in UTC.
$actualTime = strtotime($strDate);
Here’s a little demo script that might help elucidate:
Output looks like:
The first line has strtotime operating in America/New_York (since that’s the default).
The second line has an explicit timezone set in the input to strtotime.