date - php localtime() function returns UTC time instead of local time
Get the solution ↓↓↓in php.ini I setdate.timezone = "Europe/Amsterdam"
. Restarted Apache, the output inphpinfo()
shows the correct timezone (Europe/Amsterdam).
Now when I printdate('H:i')
I get the UTC time, which is 2 hours behind our local time.
But when I printlocaltime(time(),true)
, I still get the UTC time, while I expected the local time.
Output oftimedatectl
is:
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.
Update:
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:
date_default_timezone_set('Europe/Amsterdam');
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.
Answer
Solution:
After all I didn't find how to set this right, but added this workaround before the time-dependent code:
date_default_timezone_set('Europe/Amsterdam');
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object of class stdclass could not be converted to string
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.
Similar questions
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.