Understanding Timezone and Time Difference in PHP

This time, we’ll discuss timezone in PHP. The consequence of timezone is the occurrence of a time difference between computer server and the date-time generated by PHP. This article will discuss how to solve that kind of problems (the time difference in PHP).

I. Timezone in PHP

When creating applications or website using PHP, certainly, we would face time difference between computer server and time generated by PHP, especially when running time-related functions like date() function.

This is due to the differences in the use of timezone between PHP and computer servers (or local computer if we run the server locally)

In PHP, the timezone by default defined in the php.ini configuration file. To see what is the current timezone, open the php.ini file located in the php directory, in the xampp, it would be in the xampp/php directory. Furthermore, find words like timezone.

In mine, the php’s timezone looks like follows:

Solve Time Difference in PHP

The default timezone can also be found by running the phpinfo() function

Default Timezone in PHP

on the above figure, the timezone is Europe/Berlin, which is GMT + 1, so if I execute the date() function, there will be a 6-hour difference in time, because I live in Indonesia that has GMT + 7 timezone.

<?php
echo date('d-m-Y H:i:s'); // Result: 2017-01-23 01:56:59

The above script executed at 07:56:59, so for the GMT + 1 timezone, the output will be 01:56:59

In addition, we can also get the current timezone by running the date_default_timezone_get() function:

<?php
echo date_default_timezone_get(); // Output: Europe/Berlin

II. Changing The Default Timezone in PHP – Solving The Time Difference in PHP

There are several ways to change the default timezone in PHP – solving the time difference in PHP -, such as using particular functions, changing the configuration within php.ini file, and changing the timezone manually.

1. Changing The Default Timezone in PHP Using ParticularFunctions

In PHP, we can use several functions to change the timezone, that are: date_default_timezone_set() and ini_set(). Consider the following example:

<?php
echo 'Default Timezone: ' . date('d-m-Y H:i:s');
date_default_timezone_set('Asia/Jakarta');
echo 'Indonesian Timezone: ' . date('d-m-Y H:i:s');

The output is:

Default Timezone: 17-01-2017 02:06:09
Indonesian Timezone: 17-01-2017 08:06:09

An example of using  the ini_set() function:

<?php
ini_set('date.timezone', 'Asia/Jakarta');
echo 'Indonesian Timezone: ' . date('d-m-Y H:i:s');

This change known as seasonal timezone, means that this timezone configuration works on execution time of the PHP scripts. So it would not work if we use PHP CLI.

The both functions will affect scripts that executed after they are defined. So, in order to affect the whole scripts, we have to write these functions at the very beginning of our PHP script.

2. Changing The Default Timezone In PHP by Changing The Configuration File

The use of date_default_timezone_set() and ini_set() function will only affect scripts that executed after this function called, even though we put at the beginning of the script, in a complex system, there may be scripts that are not affected.

Because of this, we may want change timezone globally through the php.ini configuration file. To do so, open the php.ini file and find the word date.timezone, then change the values as desired, for example Asia/Jakarta

Timezone Asia-Jakarta in PHP

Restart the Apache server to see the effect.

This method only work if we have right to access the php.ini file, such as on local or network computer

3. Changing the Timezone In PHP – On Demand

The third method is changing the date-time only when needed (on demand). To do this, we can use gmdate(). This function is same as the date() function except that the gmdate() function will return time in Greenwich Mean Time (GMT + 0), for example:

<?php
echo date('Y-m-d H:i:s'); // 17-01-2017 02:35:05 - Default timezone on my server: GMT + 1
echo gmdate('Y-m-d H:i:s'); // 17-01-2017 01:35:05 - GMT + 0

Furthermore, to transform into a GMT + 7 timezone, we add 7 hours in seconds to the second argument of gmdate() function as follows:

<?php
echo 'Default Timezone: ' . date('d-m-Y H:i:s');

$timezone = time() + (60 * 60 * 7);
echo 'Indonesian Timezone: ' . gmdate('d-m-Y H:i:s', $timezone);

The result is:

Default Timezone: 17-01-2017 02:39:10
Indonesian Timezone: 17-01-2017 08:39:10

In the above example, the time() function will produce a timestamp on GMT + 0 timezone, so by add 7 hours we’ll get GMT + 7 date.

4. Changing the Timezone In PHP On Demand – Using DateTime Class

The fourth method is to use DateTime and DateTimeZone classes. Both classes exist since PHP version 5.3.0 and up. The example looks like the following:

<?php
$timezone = new DateTimeZone('Asia/Jakarta');
$date = new DateTime();
$date->setTimeZone($timezone);

echo 'Indonesian Timezone: ' . $date->format('d-m-Y H:i:s') . '<br/>';
echo 'Default Timezone: ' . date('d-m-Y H:i:s');

The result:

Indonesian Timezone: 17-01-2017 09:52:42
Default Timezone: 17-01-2017 03:52:42

III. Wrap Up

When working with functions that related to date and time, always consider the timezone, because by default it will always give different result in date and time compared to the computer server.

There are several methods to change the timezone in PHP, to change it permanently, change the configuration in the php.ini file.

Related Post

Leave a comment

Like Us

Social