Time Zones in mySQL and PHP

Status
Not open for further replies.

stepan11s

New Member
Messages
4
Reaction score
0
Points
1
I'm sorry if I'm asking something that someone has already answered but I've searched the forums. If I understand correctly, X10 servers are located on the East coast of the USA. I am also located in this region.

When inputting data into mySQL and using a field with CURRENT_TIMESTAMP default value, mySQL will produce a time stamp that is 1 hour behind my current time. I'm assuming this is because of daylight savings.

When inputting this code in php I also have a time that is 1 hour behind.
PHP:
echo date("HiTe");
The output is:
2330CDTAmerica/Chicago

Questions:
1. Are mySQL and php outputs always going to read the EST time without regard of daylight savings, always UTC - 5 hours?
2. When the php output reads CDT (which is equal to EST) is that just irrelevant? Why does it show Chicago if X10 servers are in NY?
3. Most importantly, will anything change when daylight savings is off and the East coast goes back to EST? I'm fine with php output reading CDT as long as the time is really EST. In other words, when the East coast goes back to EST (daylight savings off) will my PC time = mySQL = php output?
4. Finally, any ideas as to why this code does not properly convert the EST time to UTC? Note: Record 10 was added at 12:45AM EST / 4:45AM UTC
PHP:
$now = mysql_result(mysql_query("SELECT created FROM listings WHERE id = 10"), 0);
echo $now . '<br>';
$date = new DateTime($now, new DateTimeZone('EST'));
echo $date->format('Y-m-d H:i:s') . '<br>';
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:s');
It shows the UTC time one hour behind. Why is DateTimeZone('EST') seeming to take daylight savings into consideration? Output below:
2013-09-19 23:45:34
2013-09-19 23:45:34
2013-09-20 03:45:34


Thanks for your help.
 

stepan11s

New Member
Messages
4
Reaction score
0
Points
1
Thank you both for your help. This clarifies most of my questions.

bdistler, I tried the link, but for some reason the code didn't work for me.
PHP:
mysql_query("SET time_zone = '-5:00'");

Regardless, i'm not sure it will help me. Now I understand I'm on the Vox server and use CDT. But since the servers are on a daylight savings time zone, it doesn't really seem possible to alter the time considerately. Meaning, right now the time zone is -5 UTC but later on it will be -6.

Thank you once again, I now know what to expect when daylight savings is over.
 

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
Points
63
#1 - you may have mixed MySQL_FUNCTIONS with PDO - if so you will need to use a PDO query

#2 - use a timezone not a UTC offset to auto-adjust for daylight savings
 
Status
Not open for further replies.
Top