Why is the default PHP time zone "Europe/Moscow" in my system? - php

I have been running PHP on many Ubuntu versions and all of them exhibit the same "feature". When I dump the output of the phpinfo() function to a web page, I see the following:
Default timezone: Europe/Moscow
when both php.ini files (/etc/php/7.?/apache2/php.ini and /etc/php/7.?/cli/php.ini) have the date.timezone setting commented out, as it is the case by default.
My system's time zone has always been set correctly to Europe/Istanbul:
$ file /etc/localtime
/etc/localtime: symbolic link to /usr/share/zoneinfo/Europe/Istanbul
However, PHP thinks my local time zone is Europe/Moscow instead. Currently, these two time zones are similar, but this was not so in the past and may not be so in the future.
To solve, this problem each time these php.ini files are being updated (for example, after a patch or system upgrade) I have to manually edit them and set:
date.timezone = Europe/Istanbul
What is the reason of this strange behavior and what can I do to solve this problem?
Currently, I am on Ubuntu 19.10 with PHP 7.3, but the problem exists in previous OS and PHP versions too.

You said yourself that Moscow and Istanbul are in the same timezone. If you are worried that your PHP programs may suffer functionality in the event that this were to ever change you can may use the function
date_default_timezone_set ( string $timezone_identifier ) : bool
to override the php.ini file. I have included a link to the documentation.

Related

date function disregards the default timezone

Following best practice to not modify php.ini directly but to have separate files, I have the following file:
$ cat /etc/php/7.0/apache2/conf.d/99-timezone.ini
[Date]
date.timezone="America/Los_Angeles"
I can see that the ini file is being loaded as evidenced by this phpinfo screenshot. However in the date section only one of the two directives has updated to show America/Los_Angeles and one is UTC. I don't even know where UTC is coming from, as the system's time is not UTC:
$ timedatectl
Local time: Tue 2017-11-07 18:36:56 PST
Universal time: Wed 2017-11-08 02:36:56 UTC
Timezone: America/Los_Angeles (PST, -0800)
(snip)
I see the following outputs when I run each of these:
var_dump(ini_get('date.timezone')); //string(19) "America/Los_Angeles"
var_dump(date_default_timezone_get()); //string(3) "UTC"
var_dump(date('e')); //string(3) "UTC"
How do I get the first two to local time?
Looking at your second screenshot, the only way for that to happen (that I know of) is if date_default_timezone_set('UTC'); has been called at runtime. It alters the 'Default Timezone', causing it to differ from the date.timezone value in your .ini files.
So look for things like:
auto_prepend_file= in your .ini file. This directive causes a script to be automatically prepended before every PHP file that is processed. If this is in use and the PHP file that it loads sets the timezone, it could cause this.
Are you using a PHP framework? If so, search the framework's PHP files for date_default_timezone_set. As an example, WordPress forces the timezone to UTC.
If not using a framework, are you using any PHP libraries that may contain date_default_timezone_set. If so, that could be the trouble.
The fact that it's showing up like this in your phpinfo() output suggests it is an auto_prepend_file doing this, because I'm assuming that you're checking phpinfo with a file that only contains <?php phpinfo(); — no framework or library.

default time zone is not changing using php or php.ini

i'm trying to change the default time zone using php but not working
date_default_timezone_set('Asia/Riyadh')
and tried using php.ini
date.timezone="Asia/Riyadh"
but in phpinfo i get this
Default timezone Europe/Berlin
date.timezone Europe/Berlin Europe/Berlin
and when inserting time to MySQL using CURTIME() i don't get Asia/Riyadh time and get a wrong time
update (timezone changed after awhile but using CURTIME() to insert date into mysql doesn't enter the right time
in php.ini
date.timezone="Asia/Riyadh"
and in my script
$nowtime = date("G:i:s");
$nowdate = date("Y-m-d");
Yes, you must always restart your server when making any changes to your php.ini file.
Another thing to look into if you are positive that you are updating the correct .ini file is to look at the "Additional .ini files parsed" in your phpinfo(). Sometimes you will find that certain programs or updates may add additional files here and it will override your php.ini settings.

PHP wrong date/time

PHP date() & time() return incorrect time:
When date.timezone = "Europe/Riga" the time returned by date() was 03-12-2011 08:57:12, but system time was 03-12-2011 01:57:12 (timezone Europe/Riga - correct time at that moment).
When I changed timezone to "Europe/London", the time changed to 03-12-2011 06:57:12 ( actual time 02-12-2011 23:57:12 )
Time returned by date / hwclock --show was correct (03-12-2011 01:57:12 with system timezone set as Riga)
OS: Debian 6.0
I have checked most of the questions regarding similar issues on SO/Google, but they all seem to have wrong timezone specified.
As far as I can tell there is problem between php -> os.
Of course, because the incorrect time offset is always constant I could subtract difference, but it is not a proper solution.
Any ideas will be greatly appreciated.
Reading PHP manual seems that behaviour of date.timezone is affected by settings in php.ini.
There is another way to set the default timezone in all date/time function and it's the date_default_timezone_set. Try to set it with:
date_default_timezone_set('Europe/Riga');
instead of your date.timezone code.
The problem looks similar to what I have seen on one of my servers. Looks to like bug in php 5.3.2-1. Try to run the php script in the bug report and post your results.
The timezone of the system could be wrong. This results in a shift in a time given by the PHP date() function, althought both php date.timezone (in php.ini) and system time of the server are correct.
I had some problem with timezone too. This can be handy for someone.
In my case, Chile Sumer Time CLST returned wrong timzeone offset.
Updating the timezonedb worked for me.
Go to: https://pecl.php.net/package/timezonedb
For Widnows Download the newest version of dll, copy into "ext" directory. Edit the php.ini and put below line:
extension=php_timezonedb.dll
For Linux You can use :
pecl install timezonedb
and in php.ini put:
extension=php_timezonedb.so
This comment for Azerbaijan timezone
I have problem with 'Asia/Baku' on php 7.2 or php 7.1 version.
I solved problem change to 'Europe/Baku'
You have to set date.timezone in php.ini and restart your server
http://php.net/manual/en/timezones.php
PHP time is based on epoch time scale which uses GMT and later UTC. Most people now refer to it as unix time. Because PHP uses unix time, timezones are not used. I believe subtracting the timezone hours in seconds is the correct method to adjust for the differences.

Wrong PHP date() output in wamp server

The problem is that date('r') returns wrong time for my timezone.
php.ini timezone setting:
date.timezone = Europe/Kiev
date_default_timezone_set('Europe/Kiev') in my script solves the problem.
So what's wrong with WAMP?
I suggest always using date_default_timezone_set() from script
e.g.
date_default_timezone_set('Europe/Kiev');
or
ini_set('date.timezone', 'Europe/Kiev');
...to avoid PHP guessing timezone.
It comes handy when you transfer code to different server(s), for example, outside of Ukraine. This line should help you to avoid unexpected (wrong) results if date.timezone is not set in php.ini or its setting is incorrect. It's also handy when you can't access and/or modify php.ini (shared hosting).
Also, be sure that you've not used ; at the and of line in php.ini.
Restart server after changing php.ini.
Edit php.ini and restart Apache:
left click to WampServer in tray icon
open php.ini (go to PHP -> php.ini)
set new date.timezone value
;date.timezone = UTC
date.timezone = Europe/Kiev
restart Apache ( go to Apache -> Service -> Restart Service )
check value of date.timezone by phpinfo();
You need to reload the configuration / restart the server after editing your php.ini file.
According to the documentation of date_default_timezone_get, the date.timezone configuration option can be overridden by setting the TZ environement variable (which, in turn, can be overridden by calling date_default_timezone_set). From your description, I suspect that the TZ environement veriable is set.
by default it shows GMT time you can change for your region with following code
date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time
List of Supported Timezones
http://www.php.net/manual/en/timezones.php
I know this is an old question.
If you are using a PHP framework, you might want to check the config file of the framework. For example in Laravel, open the config/app.php and you will find timezone there. Set it to your timezone.
Open your php.ini with maybe notepad++, sublime text...
Add this line to the file: date.timezone = "X" Where X is the your time-zone of wish. Get a list of supported timezones here: http://php.net/manual/en/timezones.php
... That should do it.
3. Restart your server

get server time in php - timezone issues

on shell, my server time is (simple date in bash):
Sun Aug 29 10:37:12 EDT 2010
when I run the code php -r "echo date('Y-m-d H:i:s');" I get: 2010-08-29 10:37:10
but when I execute a php script that has echo date('Y-m-d H:i:s'); I get a different time- since php thinks the timezone is UTC (server time is EDT).
My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.
Is there a simple way to get the server time- at the timezone that is set for the server?
According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:
* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)
UTC is the default if all of those fail, so that's probably a good starting point.
The server should always have a timezone specified in the php.ini file. PHP 5.3 even raises notices if that's not the case and the DateTime extension will throw exceptions if you attempt to build objects without explicitly specifying a timezone (with a DateTimezone object) or having a default one (either through date.timezone or date_default_timezone_set).
By what you're describing, the most likely scenarios are:
Different php.ini files are being used in the two scenarios. In the first case, no default timezone is specified, so PHP attempts to infer one from the system-wide timezone of the machine.
The (second) script calls date_default_timezone_set('UTC') or equivalent.
Late for the party, but if you want to set the timezone without edit php.ini, you can put in the php code one of these strings:
date_default_timezone_set('GMT');
date_default_timezone_set('Europe/Zurich');
date_default_timezone_set('America/New_York');
Have you tried using date_default_timezone_set()
American timezones can be found here http://www.php.net/manual/en/timezones.america.php

Categories