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:

  1. Is it a big performance-hit, to leave my fixing line in the top of the functions-file: date_default_timezone_set ( 'Europe/Copenhagen' );
  2. 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:

  1. Is it a big performance-hit, to leave my fixing line in the top of the functions-file: date_default_timezone_set ( 'Europe/Copenhagen' );
  2. Can anyone figure out, why the timezone is set to UTC instead of what the value I've set in the WordPress-settings-page?
Share Improve this question asked Mar 24, 2021 at 16:21 ZethZeth 9282 gold badges13 silver badges43 bronze badges 2
  • 1 Why would it be anything other than UTC? $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
  • See this answer: wordpress.stackexchange/questions/30946/… – vancoder Commented Mar 24, 2021 at 16:49
Add a comment  | 

1 Answer 1

Reset to default 3
  1. ... 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.

  1. 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)