Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the Timezone for PHP in the php.ini file

Tags:

php

datetime

I'm trying to change the default PHP timezone to Asia/Calcutta by accessing the /etc/php5/cli/php.ini file and changing

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

to

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Calcutta"

However, when I try to view current timezone under phpinfo(), the timezone follows the Europe/Berlin timezone.

I have tried stopping and restarting the Apache server, but that didn't seem to have changed the settings.

Is there something I'm missing in the process?

PS: I'm using PHP 5.6.8 currently under XAMPP.

like image 568
Manas Chaturvedi Avatar asked Aug 26 '15 10:08

Manas Chaturvedi


People also ask

How can I modify the PHP timezone setting for my website?

To change the timezone for your website, go to your Site Tools > Devs > PHP Manager. Check a full list of the available timezones at this URL.

How can I get timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.

What timezone is UTC for PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.


4 Answers

I tried all the other possible solutions posted, but none of them helped. This is what helped me save my timezone settings:

1) Go to your phpinfo() page and search for Loaded Configuration File and open the php.ini 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.

like image 140
Manas Chaturvedi Avatar answered Oct 21 '22 11:10

Manas Chaturvedi


You can change it in the code without touching the ini file, at the beginning of your code add:

date_default_timezone_set('Asia/Calcutta')
like image 27
Daniel Krom Avatar answered Oct 21 '22 10:10

Daniel Krom


You are changing the wrong file. The file /etc/php5/cli/php.ini is used by cli - command line interface. Don't forget to restart your web-server after you update the right one: sudo service apache2 restart or sudo service php5-fpm restart

File locations for different OS:

OS                               ->  Location

windows(With Xampp Installation) -> /xampp/php/php.in 

Mac OSX                          -> /private/etc/php.ini

Linux                            -> /etc/php.ini

                                 -> /usr/bin/php5/bin/php.ini

                                 -> /etc/php/php.ini

                                 -> /etc/php5/apache2/php.ini
like image 10
miller Avatar answered Oct 21 '22 11:10

miller


I had the same problem and discovered that there are two date.timezone statements in php.ini in my version of XAMPP out of the box. The first statement one is commented out, but the second one is set to Europe/Berlin. So you if you edit your php.ini and search for timezone and logically uncomment the first statement with your time zone, the second statement overrides that back to Europe/Berlin.

like image 2
Shane Skidmore Avatar answered Oct 21 '22 12:10

Shane Skidmore