Set Same Time Zone in PHP & MySQL

Time Zones are important if you’re creating a social app which will include chat, social network and others which is interacted with the society.

There are many time zones and you have to choose one to set in your server. I would totally recommend using UTC instead of your time zone in server.

Web Server and Database server are different and they each have their own configuration. This includes the time zone too. In this short post, I’m going to show how you can set the same time zone in both of these servers.


This is the most simplest one :


Do the above before any other code.


You can set timezone in MySQL with this small SQL code :

SET GLOBAL time_zone = '+00:00';

But in servers like OpenShift, the above won’t work as the time zone will be resetted back after a server restart. So, the above is no avail in hosting providers like this.

So, we must use PHP itself to set the time zone in MySQL server. Execute the following SQL code just after the opening of MySQL connection :

SET time_zone = '+00:00';

An example in PDO :

$dbh=new PDO('mysql:dbname=mydb;host=localhost;port=3306',$username, $password);
$dbh->exec("SET time_zone='+00:00';");

Any other SQL operations must only follow after the above code execution.