Is there a PHP equivalent of `date_default_timezone_set ()` that works in MySQL?

This works fine in PHP

date_default_timezone_set('my/timezone');

$actualDate = date('Y-m-d');

      

Now when you make requests, I like to do

INSERT INTO articles (title, insert_date) VALUES ('My Title', now())

      

The problem with this: now()

in MySQL it is different from what it would be if it was calculated in PHP (and therefore against the set timezone).

Is there a way, say an SQL query, to set the default timezone for MySQL?

thanks

+1


a source to share


3 answers


http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html



If you have the SUPER privilege, you can set the value of the global clock server at runtime using this statement:

mysql> SET GLOBAL time_zone = timezone;

Time zone per connection. Each client that connects has its own time zone, specified by the session_zone variable. Initially, the session variable takes its value from the global time_zone variable, but the client can change their own timezone using this statement:

mysql> SET time_zone = timezone;

+3


a source


Something you might try is to store all your dates / times in UTC. So you would use

date_default_timezone_set('UTC');
$actualDate = date('Y-m-d');

      

and



INSERT INTO articles (title, insert_date) VALUES ('My Title', UTC_TIMESTAMP())

      

Then you have to convert UTC date / time to user's timezone before displaying it.

+1


a source


I usually install my MySQL server at GMT and then use gmdate instead of date in PHP. Saves everything to GM Time, so if you switch to a different server or have 2 in different time zones, things won't work out.

0


a source







All Articles