admin管理员组文章数量:1299988
The timezone-setting (in 'Settings' >> 'Generel' >> 'Timezone'), seems to be ignored. I've set it to be 'Copenhagen' (value: Europe/Copenhagen
).
But the servers time is still 1 hour ahead of the actual time in Denmark.
Debugging
If I simply add these lines in the top of my functions.php-file:
$datetime = new DateTime();
$datetime_str = $datetime->format( 'Y-m-d H:i:s' );
error_log( $datetime_str );
Reload a random page and go to my error-log, then I get this result:
[24-Mar-2021 16:12:08 UTC] 2021-03-24 16:12:09
'UTC'?! Hmm...
But if if add replace above-written three lines with this:
date_default_timezone_set ( 'Europe/Copenhagen' );
$datetime = new DateTime();
$datetime_str = $datetime->format( 'Y-m-d H:i:s' );
error_log( $datetime_str );
Then it outputs this:
[24-Mar-2021 17:12:13 Europe/Copenhagen] 2021-03-24 17:12:13
'Europe/Copenhagen' -> Yep. And the time is correct as well.
And if I try to set it to some other value (like 'UTC+8'), then it still says: [24-Mar-2021 16:12:08 UTC] 2021-03-24 ...
in the debug-log.
Considerations
It could be somewhere in my theme or in a plugin. But it seems excessive to start to deactivate all plugins and set another theme, just to debug this minor thing.
So my question is:
- Is it a big performance-hit, to leave my fixing line in the top of the functions-file:
date_default_timezone_set ( 'Europe/Copenhagen' );
- Can anyone figure out, why the timezone is set to UTC instead of what the value I've set in the WordPress-settings-page?
The timezone-setting (in 'Settings' >> 'Generel' >> 'Timezone'), seems to be ignored. I've set it to be 'Copenhagen' (value: Europe/Copenhagen
).
But the servers time is still 1 hour ahead of the actual time in Denmark.
Debugging
If I simply add these lines in the top of my functions.php-file:
$datetime = new DateTime();
$datetime_str = $datetime->format( 'Y-m-d H:i:s' );
error_log( $datetime_str );
Reload a random page and go to my error-log, then I get this result:
[24-Mar-2021 16:12:08 UTC] 2021-03-24 16:12:09
'UTC'?! Hmm...
But if if add replace above-written three lines with this:
date_default_timezone_set ( 'Europe/Copenhagen' );
$datetime = new DateTime();
$datetime_str = $datetime->format( 'Y-m-d H:i:s' );
error_log( $datetime_str );
Then it outputs this:
[24-Mar-2021 17:12:13 Europe/Copenhagen] 2021-03-24 17:12:13
'Europe/Copenhagen' -> Yep. And the time is correct as well.
And if I try to set it to some other value (like 'UTC+8'), then it still says: [24-Mar-2021 16:12:08 UTC] 2021-03-24 ...
in the debug-log.
Considerations
It could be somewhere in my theme or in a plugin. But it seems excessive to start to deactivate all plugins and set another theme, just to debug this minor thing.
So my question is:
- Is it a big performance-hit, to leave my fixing line in the top of the functions-file:
date_default_timezone_set ( 'Europe/Copenhagen' );
- Can anyone figure out, why the timezone is set to UTC instead of what the value I've set in the WordPress-settings-page?
1 Answer
Reset to default 3
- ... why the timezone is set to UTC instead of what the value I've set in the WordPress-settings-page
WordPress calculates offsets from UTC, therefore WordPress sets the default time zone to UTC and not the specific time zone you selected in the Settings → General admin page.
- Is it a big performance-hit, to leave my fixing line in the top of the functions-file:
date_default_timezone_set ( 'Europe/Copenhagen' );
Maybe it's not a big one, but even so, you should not change the default time zone.
Because if you do, then the following (critical issue) will be reported by the Site Health tool (wp-admin
→ Tools → Site Health):
PHP default timezone was changed after WordPress loading by a
date_default_timezone_set()
function call. This interferes with correct calculations of dates and times.
However, in certain cases, you may use date_default_timezone_set()
, but you should restore the default time zone back to UTC once you've done with your code:
date_default_timezone_set ( 'Europe/Copenhagen' );
$datetime = new DateTime();
$datetime_str = $datetime->format( 'Y-m-d H:i:s' );
date_default_timezone_set( 'UTC' ); // restore it to UTC
But then, WordPress provides its own date/time functions which apply the time zone set via the admin settings page, e.g. wp_date()
(or in the past, date_i18n()
), so you could simply utilize those functions. E.g.
// Display current time in MySQL format using wp_date():
$datetime_str = wp_date( 'Y-m-d H:i:s' );
// Or you could just use current_time():
$datetime_str = current_time( 'mysql' );
本文标签: performanceTimezone is wrong when picking city (Copenhagen)
版权声明:本文标题:performance - Timezone is wrong when picking city (Copenhagen) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741656529a2390790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$datetime->format
is straight PHP - it is not specific to WP, or aware of WP settings. You have to tell it what time zone to use. – vancoder Commented Mar 24, 2021 at 16:32