How to Set/change Default Timezone in PHP Script?


May be PHP is displaying a different date time than your local time or you may want to change current date time to a different one.


To change the date time in your PHP script, you need to replace the current timezone with your desired timezone. When you change the timezone, all the date/time functions you’ll use in your script will display time according to the new timezone.

You can apply any of the following two solutions.

Method 1: Using date_default_timezone_set() function

As the name says, the date_default_timezone_set() function sets the default timezone. All you have to do is assign the new timezone as the parameter in the function. See the example below-


How it works:
In the above line of code, we set the default timezone as “Europe/Rome”. After this line, any date/time functions you write in this script will work according to the local time of Rome, Italy.

To see your current timezone, use date_default_timezone_get() function. See the following line of code-

echo date_default_timezone_get();

[wpdm_file id=134]


You may need different timezone than the one used in the above example. To find out your timezone, go to list of supported timezones page and choose your timezone.

Method 2: Using DateTimeZone and DateTime classes

Using SPL(Standard PHP class) to set the timezone is 2 steps process-

  • First create an instance of DateTimeZone class with your desired timezone as parameter.
  • Then, create another instance of DateTime class with the instance of the DateTimeZone object you created in the above step.

See the example below-

$dtzObj = new DateTimeZone('Europe/Rome') ;
$dtObj = new DateTime(NULL, $dtzObj);
echo  $dtObj->format('Y-m-d h:i A');

[wpdm_file id=135]

2014-06-14 06:31 PM

Now, you can use this DateTIme instance ($dtObj – which is the representation of local Rome time) to manipulate different date and time based operations.

The new timezone you set by the above methods will only affect the current script. If you want to put the effect in all the scripts, you need to set the new timezone in php.ini file. To accomplish this, see the tutorial – How to Set Default Timezone in php.ini File?