I set the right server time with the help of this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-16-04

When I type “date” in the console, the correct time is now shown.
But the PHP date(‘H:i’) command still shows 2 hours earlier. I have reset Apache2.

How can this happen and what can I do about it?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
6 answers

Hi @Roelandvo,

You’ll need to update the default timezone of your PHP in your php.ini. I’ll recommend setting it like so :

date.timezone="Europe/London"

Now, the default is Europe/London however you can set it to any timezone you wish.

Please make sure you’ve added it to your PHP.ini as this is related to your PHP rather than the server itself.

Regards,
KFSys

Hi @KFSys,

Thanks for your help.
It doesn’t work until now; I changed the php.ini to:

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

I also checked via phpinfo() to be sure that this is the right ini file.
Then restarted the server with

sudo /etc/init.d/apache2 restart

But the time is still 2 hours behind.

@KFSys,

If I reopen the file, the changes seem saved, it shows:

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

(yesterday I forgot the quotes, I added them now and restarted Apache again, but still 2 hours behind)

edited by MattIPv4
  • That’s good to hear but have you checked from your browser via phpinfo() if the change has taken place?

    I feel like it’s possible the info hasn’t been accepted for some reason

Hi KFSys, this is the timezone part in phpinfo():

date/time support   enabled
timelib version 2017.09
"Olson" Timezone Database Version   0.system
Timezone Database   internal
Default timezone    UTC
Directive   Local Value Master Value
date.default_latitude   31.7667 31.7667
date.default_longitude  35.2333 35.2333
date.sunrise_zenith 90.583333   90.583333
date.sunset_zenith  90.583333   90.583333
date.timezone   Europe/Amsterdam    Europe/Amsterdam

So date.timezone seems correct, maybe it’s going wrong in the values above it?

Hi @KFSys ,

Thanks for your reply!
So as a test, I changed it to Detroid, this gives the same effect; this is the phpinfo():

date/time support   enabled
timelib version 2017.09
"Olson" Timezone Database Version   0.system
Timezone Database   internal
Default timezone    UTC
Directive   Local Value Master Value
date.default_latitude   31.7667 31.7667
date.default_longitude  35.2333 35.2333
date.sunrise_zenith 90.583333   90.583333
date.sunset_zenith  90.583333   90.583333
date.timezone   America/Detroit America/Detroit

And the time is still 2 hours behind our local time so the change seems to have no effect..

Thanks,

  • Okay, it seems it’s connected mostly to your Droplet rather than PHP in that case.

    Can you SSH to your droplet and type in the following command

    timedatectl
    

    Another way to check the time zone is to view the path the symlink points to using the ls command:

    ls -l /etc/localtime
    

    To view all available time zones, use the timedatectl command or list the files in the /usr/share/zoneinfo directory:

    timedatectl list-timezones
    

    Once you identify which time zone is accurate to your location, run the following command as root or sudo user:

    sudo timedatectl set-timezone <your_time_zone>
    

    For example, to change the system’s timezone to America/New_York you would type:

    sudo timedatectl set-timezone Europe/Amsterdam
    

    To verify the change, invoke the timedatectl command again:

    timedatectl
    

    Regards,
    KFSys

Thanks again for your help!
This is interesting. timedetectl was already set to Europe/Amsterdam, but this is the result:

 Local time: Thu 2020-07-09 09:15:26 CEST
 Universal time: Thu 2020-07-09 07:15:26 UTC
 RTC time: Thu 2020-07-09 07:15:27
 Time zone: Europe/Amsterdam (CEST, +0200)
 System clock synchronized: yes
 systemd-timesyncd.service active: yes
 RTC in local TZ: no

So the local time is correct, but php seems to use the Universal or RTC time in the time() function.
Now I found the PHP localtime() function:
https://www.w3schools.com/php/func_date_localtime.asp

But this code:

echo 'time: ' . date('H:i');
echo "<br>";
print_r(localtime(time(),true));

even gives this result:

time: 07:35
Array ( [tm_sec] => 15 [tm_min] => 35 [tm_hour] => 7 [tm_mday] => 9 [tm_mon] => 6 [tm_year] => 120 [tm_wday] => 4 [tm_yday] => 190 [tm_isdst] => 0 )

So the localtime() function doesn’t seem to do anything. (changing back the timezone in php.ini from Detroid to Amsterdam doesn’t have any effect on the output above (didn’t forget to restart Apache)).

Submit an Answer