26
Sep

WordPress Problem: PHP mktime() Not Working/Broken – Wrong Timezone

Quick note for WordPress developers, this one had me scratching my head.

If you use mktime() in your plugin code you will notice the timezone will always reflect GMT. There is a reason for this.

Notice that in settings, WordPress asks you for your locale. It then tells you what the time is in GMT and then what the new locale time is. Somehow, WordPress overrides php.ini, system time, etc and thinks its GMT when using mktime(). You cant change it anywhere, not in .htaccess ,nothing. It’s probably embedded deep within the core of wordpress somewhere, and has something to do with plugin functions called within the Loop.

Anyways, how do we get the current linux timestamp that mktime() normally so easily creates for us? Use the following wordpress function:

current_time(“timestamp”);

This will give you your standard linux timestamp. It also has the ability to create a mysql timestamp using the format:

current_time(“mysql”);

Test it out, but don’t try using a separate .php to test as mktime() works fine outside of the Loop.

Hope this helps!!

  • steve

    Thanks…this was throwing me for a loop and I tried all the usual fixes like .htaccess, etc.

    • http://www.geilt.com Geilt

      Your welcome! Took me hours to figure out what the heck was going on.

Find Me
Join Me
Post Calendar
May 2013
M T W T F S S
« Apr    
 12345
6789101112
13141516171819
20212223242526
2728293031  
Twitter
43 years ago
About Geilt
Alexander Conroy is Chief Optimization Officer and Co-President of Esotech in Miami Florida
Google+ Statistics
Empire Avenue
© Copyright 2010-2013 Geilt. All rights reserved. Created by Dream-Theme — premium wordpress themes. Proudly powered by WordPress.