When I run a simple php file called test.php (placed in the root of the wp installation) that contains something like that:
<?php echo date('H:i:s'); ?>
The result is right: 15:30:00
and the timezone is set also correctly to Europe/Prague which is UTC+2 time
However, if I place the same line within my e.g. footer or sidebar or even index file of my template than the result is 13:30:00
which is UTC time.
The same in my custom plugin in WP admin. the time is 2 hours late.
The time in admin options is set correctly to UTC+2.
What is funny that if I create a new post or page the time is correct 15:30:00. I don’t get it.
Any idea why this happens?
I have the latest fresh WP installation. 3.5.1 CZ version without any installed plugins.
UPDATE:
I have tried using WP function date_i18n() like:
<?php echo date_i18n('H:i:s'); ?>
and it’s working as expected.
However:
<?php echo date('H:i:s'); ?>
and it’s still not working right. But outside wordpress date()
is working correctly.
Why is that? Why is wordpress screwing up date()
function ?
WordPress uses GMT. The only way to get around that is to declare a new timezone after the header to use for your own purposes. Kind of a pain.
Also see this answer: How to get WordPress Time Zone setting?
The accepted answer here is not the best approach. You should use WordPress’
current_time()
function instead. Changing the timezone at runtime can have unintended consequences because WordPress normalises it to (and expects) UTC.If you need to convert an existing UTC date to your local timezone, see Converting timestamps to local time.