1

Environment: PHP 7.3.13RC1; CentOS 7.7.1908; Server Timezone "Europe/Berlin"

Observation:

The following PHP script:

$date1=date_create("now");
$date2=date_create("now");
$date3=date_create("now");
$tz1=new DateTimeZone("Europe/Berlin");
$tz2=new DateTimeZone("UTC");
$tz3=new DateTimeZone("GMT");

echo "Current Set Times:","\n";
echo "Date 1: ", $date1->format(DateTime::ATOM), "\n";
echo "Date 2: ", $date2->format(DateTime::ATOM), "\n";
echo "Date 3: ", $date3->format(DateTime::ATOM), "\n";

$date1->setTimezone($tz1);
$date2->setTimezone($tz2);
$date3->setTimezone($tz3);
echo "Times with new Time Zone Set", "\n";
echo "Date 1: ", $date1->format(DateTime::ATOM), "\n";
echo "Date 2: ", $date2->format(DateTime::ATOM), "\n";
echo "Date 3: ", $date3->format(DateTime::ATOM), "\n";

Results in this output:

Current Set Times:
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T22:01:00+01:00
Date 3: 2019-12-21T22:01:00+01:00
Times with new Time Zone Set
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T22:01:00+00:00
Date 3: 2019-12-21T21:01:00+00:00

Whereas I would expect to see the following output:

Current Set Times:
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T22:01:00+01:00
Date 3: 2019-12-21T22:01:00+01:00
Times with new Time Zone Set
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T21:01:00+00:00
Date 3: 2019-12-21T21:01:00+00:00

The difference lies in the output of the second date. The time is off by one hour for the UTC time (22:01:00 instead of 21:01:00).

I am at a loss here. I tried to set a default time zone within php.ini, to no avail.

Using "date" and "date -u" gives me the correct times.

timedatectl gives me the following output: Local time: Sa 2019-12-21 22:06:49 CEST Universal time: Sa 2019-12-21 21:06:49 UTC Time zone: Europe/Berlin (CET, +0100)

Maybe someone else has an idea what is going on here.

Lord Haxor
  • 11
  • 1
  • 6
    UTC and GMT, for the purpose of calculating times in PHP, are always exactly the same thing (see https://stackoverflow.com/questions/1612148/gmt-vs-utc-dates) so should always output the same. Your results are puzzling and I cannot reproduce it - https://3v4l.org/cM26o – rjdown Dec 21 '19 at 21:42
  • I ran your code under PHP 7.2 and GMT and UTC output the same values. – ryantxr Dec 22 '19 at 05:58
  • Did you eventually figure this out? As already said, [we cannot reproduce it](https://3v4l.org/OkOcu) so something must be misconfigured in your PHP installation but I honestly can't imagine what; `UTC` is the most basic time zone :-? – Álvaro González Feb 08 '20 at 13:26
  • Sadly not. Still the same problem, though I circumvented it by not using UTC but GMT instead. And yes, I assume something within my VM is corrupt, but I am absolutely out of clues what. – Lord Haxor Feb 17 '20 at 20:08
  • Facing a similar issue while using php 7.4 with in Docker.I get the correct time as expected when using GMT but get an incorrect time when using UTC – lk404 Nov 11 '20 at 19:28

1 Answers1

0

I can't reproduce that either. Look here. The time zone for your PHP may not be set correctly.

Add this as the first line

date_default_timezone_set("Europe/Berlin");

and try again.

jspit
  • 7,276
  • 1
  • 9
  • 17
  • That did not change a thing. I already tested all the different methods to set the time zone within PHP, e.g. date.timezone within php.ini, the same within the script itself, the date_default_timezone_set. Yet the result is the same. Still the UTC version is not printing the correct time. – Lord Haxor Dec 23 '19 at 18:26