Timezone madness with WordPress

date.timezone is not working

  • The date.timezone from my htaccess is not working.
  • I've set the date.timezone in my php.ini but it's not working

On my server configuration we set the global php.ini timezone but it wasn't working on my WordPress plugin.

<?php
echo date("G:i"); // Not correct, still using UTC even though I've set "Europe/Madrid"
echo time(); // Not correct either
?>

I had changed the admin Settings to the correct timezone but still, it was using UTC. I've tried overriting the date.timezone using the .htaccess and the wp-config.php without any luck.

current_time() to the rescue

This beautiful core function will solve all your headaches: http://codex.wordpress.org/Function_Reference/current_time

How does it work?

You should use this function instead of date() or time() in your themes or plugins.

Why?

It will take into consideration the timezone set from the admin Settings page.

How do I use it?

<?php
$time = current_time(timestamp);
$date = current_time('G:i'); // since WordPress 3.9
?>

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*