Changing the MySQL timezone via PHP

If you don’t have root access in your mysql instance and want to change the time_zone, you’ll have to change it in the code:

To have the time zone setup as you would like, you would you need to execute the following command each time you make a connection:
SET time_zone=’-3:00′

Here is an example of how to achieve that in PHP:

$offset = ‘-3:00’
$db = new \PDO(‘mysql:host=localhost;dbname=test’, ‘dbuser’, ‘dbpassword’);
$db->exec(“SET time_zone=’$offset’;”);

To verify:

mysql> select now()

About vicki

Welcome to the Sovereign Republic of Vickistan. I am the President here. Lucky me! No taxes or laws yet. Lucky you!
This entry was posted in Linux. Bookmark the permalink.