37

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104
  • Have you tried it without the quote marks? `date.timezone = Asia/Calcutta` ... and make sure it's not overridden anywhere . – CD001 Aug 26 '15 at 10:52
  • @CD001 Yes, I have tried without the quote marks as well. That didn't work either. – Manas Chaturvedi Aug 26 '15 at 10:55
  • You are trying to change the timezone for your web server processes, but you are editing the CLI php.ini file (`/etc/php5/cli/php.ini`) which will change it only when running PHP from the terminal. – Etienne Apr 05 '19 at 12:19
  • It's recommended to restart your web server to reload PHP.ini settings. – Izzy Helianthus Nov 08 '19 at 02:45

11 Answers11

52

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.

Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104
  • 2
    Some examples show the value in quotes like "Asia/Kolkata" and others do not like the above. Are both acceptable? – Anthony Petrillo Sep 25 '20 at 15:35
  • like what @AnthonyPetrillo asked, are both acceptable ? I can't get mine to work – Shaho Oct 22 '21 at 18:24
  • Like @namal has answered above, you need to set the strings as defined in [timezones list](https://www.php.net/manual/en/timezones.php), then see the results reflected by`php -r 'phpinfo();'` – vishwarajanand Jan 31 '22 at 09:59
15

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')
Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
10

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
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
miller
  • 904
  • 8
  • 16
  • 3
    *The file /etc/php5/cli/php.ini is used by cli* ... well spotted :) – CD001 Aug 26 '15 at 10:59
  • I have Ubuntu installed, and I don't seem to have the following files: `/etc/php.ini` and `/etc/php5/apache2/php.ini`. Infact, I don't seem to have a `apache2` folder inside the `php5` folder at all. I do have a `cgi` folder though, and can access the `/etc/php5/cgi/php.ini`. – Manas Chaturvedi Aug 26 '15 at 11:09
  • you can view used php.ini files by running scipt with `phpinfo();` function from your browser. – miller Aug 26 '15 at 12:44
  • any luck with setting timezone? – miller Aug 26 '15 at 21:18
2

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.

2
  • phpinfo() was returning Loaded Configuration File => /etc/php/5.6/cli/php.ini.
  • My date.timezone changes were not taking effect after updating the above and restarting php-fpm (sudo systemctl reload php5.6-fpm.service).
  • phpinfo() also has an Additional .ini files parsed section, which contained /etc/php/5.6/cli/conf.d/timezone.ini.
  • After updating the timezone.ini file (date.timezone = "America/New_York") and restarting php-fpm (as above), phpinfo() correctly returned date.timezone => America/New_York => America/New_York.
1

If you are using Laravel, you should change the Laravel timezone configuration.

My case Laravel 6. Edit /config/app.php file and change this line

'timezone' => 'UTC',

to

'timezone' => env('APP_TIMEZONE', 'UTC'),

then edit .env file. add your timezone (timezone list)

APP_TIMEZONE='America/New_York'

clear configurations

php artisan config:clear
namal
  • 1,164
  • 1
  • 10
  • 15
1

For me, running systemctl restart php-fpm after editing the /etc/php-ini file solved the problem.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Lions
  • 11
  • 1
0
  1. If you 're serious about web developping get a VM going(Virtual Box + Vagrant).
  2. With XAMPP be careful. Sometimes there's more than 1 php.ini file and you might be editing the wrong one. To edit the real one, access it from the XAMPP control panel.
  3. You can use the a function in your code: date_default_timezone_set();
Patrick
  • 203
  • 1
  • 9
0

Check configuration in your system.

In my case, it was "Ubuntu"

sudo cat /etc/timezone Asia/Kolkata
sandip
  • 533
  • 9
  • 29
Ganesh Patel
  • 67
  • 1
  • 4
0

in my case, Centos 8, DirectAdmin, Openlitespeed, php 7 (lsphp server must run with Openlitespeed)

  1. Go to DirectAdmin (with admin/root password) -> CustomBuild - > SETTINGS - > Options -> PHP Settings -> php_timezone -> input your time zone and save Time zoze is list here: https://www.php.net/manual/en/timezones.php

  2. return CustomBuild -Build -> select PHP, and click build!

  3. Waite 10 minute for amazing...

Hoàng Vũ Tgtt
  • 1,863
  • 24
  • 8
0

Nothing had worked for me, I'm using FPM.

Found a file called www.conf inside /etc/php/8.2/fpm/pool.d , edited php_admin_value[date.timezone] value, restarted FPM with sudo service php8.2-fpm and it worked.

I'm using Vultr, hope this answer will help someone in the future.

Parth Parmar
  • 101
  • 1
  • 2
  • 17