Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting MySQL timezone for session

I know that using this query I can set time per session in mysql server

SET SESSION time_zone = '+04:00'

Question is how to detect this +04:00 (it can vary during year because of daylight saving time) part in PHP for my country?

enter image description here

like image 210
heron Avatar asked Jun 28 '26 20:06

heron


2 Answers

Try using the timezone name (like America/Chicago) instead of +/- a set of hours. This should take into account daylight savings time.

You will need to load zoneinfo into your mysql database if you haven't already.

like image 50
datasage Avatar answered Jul 01 '26 08:07

datasage


I will take a guess that perhaps you are in Azerbaijan? If so, you should use the IANA time zone id of Asia/Baku. This time zone alternates between a standard offset of +4:00 and a daylight offset of +5:00.

See "Time Zone != Offset" and "Time Zone Databases" in the timezone tag wiki.

If you need to do this in MySQL, you will need to load the IANA time zone database (aka "zoneinfo") into MySQL using mysql_tzinfo_to_sql. Thanks to datasage and kordirko for this tip.

But often the better approach is to only store UTC times in your database. You can do timezone translations directly in PHP using the DateTime and DateTimeZone classes. PHP also implements the same IANA time zone database, and you can find Asia/Baku on PHP's list of supported time zones.

like image 38
Matt Johnson-Pint Avatar answered Jul 01 '26 10:07

Matt Johnson-Pint