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.
PHP
This is the most simplest one :
date_default_timezone_set("UTC");
Do the above before any other code.
MySQL
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 MySQ****L 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.