PHP – Get current date and time of different time zone

In this post we will be creating a PHP script that will return the current date and time of a different time zone and print this to a webpage. To do this we will be using the ‘date()’ function which formats a Unix timestamp.

See the snippet of code which will print the current date and time to a webpage. This by default will go by your server time.

<?php
   $date = date('Y-m-d H:i:s');
   echo($date);
?>

In order to change the time zone we would use the following snippet of code, here we are using the ‘date_default_timezone_set()’ method. For a full list of PHP time zones available click here. In the example below we are using the time zone for America:Chicago.

<?php
   date_default_timezone_set('America/Chicago');
   $date = date('Y-m-d H:i:s');
   echo($date);
?>

Leave a Reply