简体   繁体   中英

Dates returned from MySQL timestamp column change times

I'm currently working on an issue where the PHP server is located in Europe but the database server is located in Mexico.

In my app, the timezone is set with:

date_default_timezone_set('America/Mexico_City');

The application doesn't set the MySQL timezone, so it appears to use the system timezone, which is America/Mexico_City .

The strange thing is when I query a timezone column in MySQL, it sometimes gives me dates using the time in Mexio and sometimes with the time in Europe.

I know that mysql stores timestamps as an integer and returns them as a date string, and I could probably solve this by explicitly setting the timezone the database connection is using to Mexico with $db->query("SET time_zone = 'America/Mexico_City'");

But my question is, why would it sometimes return the timestamps as date strings with Europe times and sometimes with date strings in Mexico times? The application doesn't use any type of specific caching.

Probably, your problem is related to this questions.

Having Timezone problems with PHP and MySQL

and

How do I set the time zone of MySQL?

Note how they use either SET GLOBAL time_zone = "your_time_zone" or SET SESSION time_zone = "your_time_zone"

Hope it helps :)

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