WordPress.org

Ready to get started?Download WordPress

Forums

Problem with DATE and related functions (9 posts)

  1. danycode
    Member
    Posted 1 year ago #

    Hallo, i'm trying to find out what is the right way to save and display a date. I need this for a plugin that i'm developing.

    At the moment i get the current date with:

    current_time('mysql')

    then i save this date into a DATETIME field in the database.

    The date in the database is perfect and matches the other date in wordpress, just like the comment_date field in the wp_comments table.

    When i want to display the date i use this:

    get_gmt_from_date($result['date'],get_option('date_format').' '.get_option('time_format') )

    But there is something wrong here, the resulting format is ok, but the displayed date is 1 hour before the right date.

    I'm working with the Setting -> General -> Timezone = UTC+1

    Thanks

  2. bcworkz
    Member
    Posted 1 year ago #

    Sorry if I misunderstand your question, these timezone issues can be confusing. Are you saying the displayed date is 1 hour before the correct GMT time? Or one hour before the correct UTC+1 time? Since the function returns the GMT or UTC time, one should expect it to be 1 hour before UTC+1.

    Summer time can also confuse things, at least that shouldn't be an issue for a while. If at all possible, in the time zone settings, you're better off choosing a nearby city that has adapted the same time policies as your locale. WP handles summer time issues automatically that way. It cannot do this when given a manual setting.

    Another confusing issue is what time zone is your server in? Perhaps it is displaying the correct time for where it is?

  3. danycode
    Member
    Posted 1 year ago #

    Hallo,

    Are you saying the displayed date is 1 hour before the correct GMT time?

    The displayed date is one hour before the saved date, example.

    In the database: 2012-11-23 17:14:19 ( This is the right GMT+1 date, UTC+1 doesn't mean the same? )

    The displayed date with the get_gmt_from_date() ( a core function ):

    result: November 23, 2012 4:14 pm

    Another confusing issue is what time zone is your server in?

    I'm using XAMPP, in php.ini the time is set to Europe/Berlin

    Perhaps it is displaying the correct time for where it is?

    With the php date() function the time displayed is the right time.

    In my previous plugins i used to add an option, so users can set an offset to adapt the displayed date, but i'd like to find the correct way to manage date without this option.

  4. danycode
    Member
    Posted 1 year ago #

    Now i'm trying to output the date with:

    mysql2date( get_option('date_format') . ' ' . get_option('time_format') , $result['date'] )

    instead of:

    get_gmt_from_date($result['date'],get_option('date_format').' '.get_option('time_format') )

    and the resulting date seem to be ok. Therefore i save the date with current_time and i output the date with mysql2date.
    If someone can confirm that this is the right method to manage date. Thanks.

  5. bcworkz
    Member
    Posted 1 year ago #

    Thank you for clarifying. My confusion seems to be my own fault, as I was talking about WP time when you are obviously asking about PHP time. My apologies.

    Still I'm confused. You store a timestamp of 17:14 for UTC+1 or GMT+1, same thing. When you call a function to get a GMT time, you should expect it to return the time in GMT, in this case essentially 16:14. I don't see the problem. If you request a time in GMT, the onus is upon you to correct it as needed for your local time, in your case, adding 1 hour to the returned time.

    mysql2date simply returns the time stored in the table. In your case, this is what you want, thus a proper method. Alternately, you could get the gmt timetamp, but you need to add the 1 hour difference for your local time. This is a good method if you do not know the timezone the data is stored in. Since you do know, it involves an extra step, and is not the best choice for your situation.

  6. danycode
    Member
    Posted 1 year ago #

    Thanks, before this topic was not clear for me that the get_gmt_from_date() function returns the time as GMT+0 / UTC+0.

    For these reasons i suppose that if i save a date into the database with:

    current_time('mysql')

    i will permanently lose the GMT time, thus and i need also save the GMT time:

    current_time('mysql', True )

    I'm looking on the wordpress database and on the wordpress core and seem to work i this way with comments and posts, not?

  7. danycode
    Member
    Posted 1 year ago #

    This is the current_time phpdoc:

    /**
     * Retrieve the current time based on specified type.
     *
     * The 'mysql' type will return the time in the format for MySQL DATETIME field.
     * The 'timestamp' type will return the current timestamp.
     *
     * If $gmt is set to either '1' or 'true', then both types will use GMT time.
     * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
     *
     * @since 1.0.0
     *
     * @param string $type Either 'mysql' or 'timestamp'.
     * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
     * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
     */
  8. bcworkz
    Member
    Posted 1 year ago #

    It is peculiar how we can get something in our mind a certain way, and it's hard to revise that view despite evidence to the contrary.

    If you take a look at Database Description, you will see WP stores both local timestamp and GMT timestamp for both posts and comments. It appears someone has anticipated the issue you face.

    Not that it really matters for this issue, but UTC and GMT are not exactly the same thing, I think they differ by several microseconds or something. The time it takes for the sun to transit a hundred some meters at the earth's surface. Certainly the same timezone though.

  9. danycode
    Member
    Posted 1 year ago #

    Thank you very much for you help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags