WordPress - How do I set error log timestamp timezone to local?
Asked Answered
Y

2

6

WordPress uses UTC / GMT for all its timestamps in the PHP error log. I would like to have the timestamps in the local server timezone. Changing the timezone in WP settings does not help; this affects time displays but not timestamps in the error log.

(I know there is a line in the wp_settings.php file which sets this, and I am given to understand changing that setting messes up parts of the WP code hardcoded to use UTC.)

I have looked at overriding the PHP error_log function to parse the time and replace it, but the two methods I found required having something extra installed (APD or Runkit). As I understand it, these are for DEV environments only, so I don't want to mess with them.

Any suggestion on how to get WP to write local timezone stamps to the error log?

Yoo answered 20/6, 2014 at 22:0 Comment(0)
P
2

There's a reason for logging time in UTC. Many time zones use daylight saving time. For those zones, there's an hour in the spring that is skipped, and an hour in the fall that is duplicated. If you were to log using the local time, there would not be an easy way to disambiguate between the duplicated values.

For example, if you are in the US Eastern time zone ("America/New_York") and you logged using local time, a value like 2014-11-02 01:30:00 could mean either 1:30 in Eastern Daylight Time, or 1:30 in Eastern Standard Time - an hour later.

If you log frequently enough, you might be able to detect this by comparing the timestamps of other items in nearby log entries. But in general, that's not a great solution because you might only log occasionally, or you might not want the overhead of analyzing more than one log entry at a time.

Besides daylight saving time - there's also the issue that if you take log files from multiple servers, they should be able to be compared uniformly. Perhaps I have web servers on the US East coast and West coast, one in Europe, and one in Japan. If there's a spike in my global traffic - I shouldn't have to do time zone conversions to line things up.

If you really must log in local time - then consider including the offset from UTC along with the timestamp. This is known as a "DateTimeOffset" in some languages, and is also part of the ISO-8601 standard. For example, "2014-06-20T01:23:45-07:00". By doing this, you at least allow for conversion back to UTC and remove any ambiguity caused by daylight saving time.

I don't know if there's a specific way to have WordPress or PHP log in this manner, but perhaps someone else can offer that as a separate answer.

Predicative answered 20/6, 2014 at 22:51 Comment(1)
This is specific to WordPress, unfortunately. WP forces all timestamps to UTC, and we need it to be local. There's only one server for this application, so there's no concern about multiple timezones to reconcile. WordPress is making the timestamps happen in 73 separate places (at least in the current version), far more than a small hack. Hopefully a WP expert has an idea on this. :)Yoo
A
1

Just set the preferred timezone in functions.php

date_default_timezone_set('Asia/Kolkata'); 

Change 'Asia/Kolkata' with your preferred timezones. PHP Official timezone

Before & after of changes

Better to use child-theme to preserve changes made in functions.php even after the theme gets updated.

Anuska answered 29/7, 2022 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.