date - php localtime() function returns UTC time instead of local timeGet the solution ↓↓↓
in php.ini I set
date.timezone = "Europe/Amsterdam". Restarted Apache, the output in
phpinfo() shows the correct timezone (Europe/Amsterdam).
Now when I print
date('H:i') I get the UTC time, which is 2 hours behind our local time.
But when I print
localtime(time(),true), I still get the UTC time, while I expected the local time.
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
Any help to get the local time is appreciated.
The output of phpinfo() is:
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" is correct, but what is the difference with "Default timezone?"
When I add this line above the script, the time is right:
But the PHP date_default_timezone_set reference page tells that "Instead of using this function to set the default timezone in your script, you can also use the INI setting date.timezone to set the default timezone.". This is what I did and doesn't work.
Setting the default timezone in the script is of course a hacky workaround; setting it in php.ini is the better way, so I'm still interested in how to fix this.
After all I didn't find how to set this right, but added this workaround before the time-dependent code:
Share solution ↓
Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.