heron
heron

Reputation: 3661

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

Upvotes: 3

Views: 8415

Answers (3)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241420

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.

Upvotes: 1

Vikas Arora
Vikas Arora

Reputation: 1666

Open your my.ini file. For windows it is in /mysql/bin/my.ini Search for the section mysqld. in this section enter -

default-time-zone = '+00:00'

+00:00 is the offset from GMT. Check out the time-zone-support page for better reference.

Upvotes: 0

datasage
datasage

Reputation: 19563

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.

Upvotes: 4

Related Questions