14

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.

2
  • 1
    What are you using for your date() value? e.g. date('Y-m-d H:i:s') Dec 3, 2011 at 0:21
  • What happens if you use DateTime class to determine the date, in conjunction with DateTimeZone? For example $d = new DateTime('', new DateTimeZone('Europe/Riga')); echo $d->format('Y-m-d H:i:s');
    – N.B.
    Dec 27, 2011 at 16:45

7 Answers 7

23

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.

1
  • Thanks. But I have already tried it without success. The fact that when I changed timezone from Riga to London, time difference was 2 hours suggests that timezone selection "works", but the problem is with the initial time value.
    – relic
    Dec 3, 2011 at 1:33
6

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.

1
  • 1
    Seems that was not an actual bug. The reporter simply had other code that was resetting the default timezone. So valid in the sense that you should watch for that. Beaware of code and .ini settings that can have impact.
    – ficuscr
    Jun 3, 2016 at 18:03
3

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.

1

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

1

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'

0
-2

You have to set date.timezone in php.ini and restart your server http://php.net/manual/en/timezones.php

1
  • This is exactly what the OP meant when he said date.timezone = "Europe/Riga" . Jan 20, 2016 at 10:43
-4

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.

1
  • Unix time is a simple count since a fixed moment in time—it's totally unaffected by time zones. Additionally, PHP has a pretty decent time zone handling; there's absolutely no reason to skip it and write your own. Jan 20, 2016 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.