WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wordpress affecting time() function (4 posts)

  1. deko
    Member
    Posted 2 years ago #

    I have a PHP script that is called in the footer of my blog pages and also at the end of a few stand-alone pages. The exact same script runs, but when called from the blog footer, it returns a different value than it does from the stand-alone pages. The script uses time() to get a time value.

    The output below is what I am seeing. The 9:41 time is correct and is returned by the script when called from the stand-alone (non-blog) pages. The 4:41 time is 7 hours ahead of the server's local time (which is 9:41) and is returned when the script is called from the blog pages.

    Is there some function WordPress uses that resets the time to UTC? If so, how do I circumvent this so my script returns the correct local time?

    I've tried modifying the time zone in wp-admin > Settings > General Settings but it makes NO DIFFERENCE which timezone I select. The time my script returns is consistently 7 hours ahead - and consistently correct when called from non-blog pages.

    May 24 2012 09:41 pm
    May 24 2012 09:41 pm
    May 24 2012 09:41 pm
    May 25 2012 04:41 am
    May 25 2012 04:41 am
    May 25 2012 04:41 am
  2. WPyogi
    Volunteer Moderator
    Posted 2 years ago #

    Have you looked at the Codex info on time/date? PHP does have a bunch of built-in time/date functions, so it's likely something is different in those two locations as to how the date is displayed...

    http://codex.wordpress.org/Function_Reference/get_the_time

  3. deko
    Member
    Posted 2 years ago #

    <?php get_the_time( $d, $post ); ?>

    Returns the time of the current post for use in PHP. It does not display the time.

    get_the_time

    So do I have to unravel whatever get_the_time is doing? Is there some function I can run, before my script runs, that will reset PHP so it will return the expected value from time()?

  4. deko
    Member
    Posted 2 years ago #

    I revised the first lines of my script to this:

    date_default_timezone_set('America/Los_Angeles');
    $timestamp = time();

    This fixed whatever WordPress did that made the PHP time() function return a value different from what time() would get from non-wp pages.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags