How to Set default timezone in PHP?

Asked : Nov 17

Viewed : 79 times

I am making a website where I need to save the date in the PHP date() function.

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

Is there any way to change the timezone for PHP or set a timezone change from somewhere in php.ini and make these values available for PHP.

Much appreciate your support.

php date datetime timezone 
3 Answers

You can do it easily just by the following two lines of PHP.


1: Method (PHP file)

<?php
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();
echo date('Y-m-d H:i:s'); 

date_default_timezone_set("Europe/Paris");
echo date_default_timezone_get();
echo date('Y-m-d H:i:s'); 
?>

 

2: Method for php.ini file

This is what helped set default timezone settings:

1) Go to your phpinfo() page and search for Loaded Configuration File and open the php.ini the file mentioned under that section.

2) Change the default timezone settings by adding your new timezone by modifying this line: date.timezone=Asia/Kolkata.

3) Save the php.ini file and stop the Apache server.

4) Restart the Apache server. The timezone settings should now be modified.

answered Nov 26


Follow the steps given below on how to set timezone in php.ini

Step 1: Create a php.ini file under your website folder.

Step 2: Change the default timezone settings by adding your new timezone.
For Example date.timezone=Asia/Kolkata

Step 3: Save the php.ini file.

answered Dec 30


PHP relies on the date.timezone value for some date/time functions executed by PHP-based applications. Sometimes you even get an error message on your web server logs that clearly indicates this necessity.

Fortunately, this is easy to accomplish and we will show you how. For this guide, we will be using CentOS 6 with root privileges.

Open the php.ini file

On CentOS, the php.ini file is located at the /etc directory.

sudo vi /etc/php.ini

Depending on the installed version of the PHP version, you will find a [Date] section like the following. If it is not there, just add a line with the "date.timezone = Your/Timezone" function.

[Date]; Defines the default timezone used by the date functions; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone date.timezone = 
 

Uncomment the date.timezone function and set its value to the timezone you need. In this case, we will use America/New_York. To see a list of supported timezones, refer to php.net

[Date] ; Defines the default timezone used by the date functions; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone date.timezone = America/New_York

Now, save and exit the php.ini file.

Restart Apache webserver

In order for the changes to take effect, you need to restart the Apache webserver:

sudo service httpd restart

Congratulations, you have set the timezone in php.ini file.

answered Dec 30


Login and Submit Your Answer
Browse other questions tagged  php  date  datetime  timezone or ask your own question.