Custom Fields
Re-Envisioned
Support General How to configure timezone for timestamp in datetime field?

  • Creator
    Topic
  • #28753
    Anselm
    Participant

    I am currently saving a custom field with timestamps and couldnt find a way to configure the timezone? Currently 2h are added using the following configuration. So when I enter 18:00 I get 20:00 for Germany.

     [
                    'type'       => 'datetime',
                    'name'       => esc_html__( 'Beginn', 'tabula_events_' ),
                    'id'         => $prefix . 'begin',
                    'desc'       => esc_html__( 'Veranstaltungsbeginn', 'tabula_events_' ),
                    'timestamp'  => true,
                    'js_options' => [
                        '' => '',
                    ],
                ],
    $format = new IntlDateFormatter(
                "de-DE",
                IntlDateFormatter::LONG, 
                IntlDateFormatter::NONE, 
                "Europe/Berlin", 
                IntlDateFormatter::GREGORIAN, 
                "EEEE', der' dd. MMMM YYYY 'um' HH:mm 'Uhr'"
            );
    $echo = datefmt_format( $format , rwmb_meta('begin','',$post_id  ));
Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #28754
    Anselm
    Participant

    I exchanged "Europe/Berlin" by date_default_timezone_get() - made it work for me though is not 100% correct.

    #28941
    vubai
    Participant

    I'm also facing this issue.

    Currently there are no time zone conversion when the timestamp is displayed in the datetime widget so the displayed date is wrongly offsetted if your current time zone is not UTC.

    I think the correct way of handling this would be to format the timestamp with the time zone set in WordPress settings in order to get a localized date to work on.

    I also tried using a custom parse function (https://trentrichardson.com/examples/timepicker/) for the widget but this is a no-go as all parameters under js_options are treated as strings. There is currently no way of specifying a custom JavaScript function in the field settings.

    #28979
    Long Nguyen
    Moderator

    Hi,

    We can just select the timezone of the site in Admin Dashboard > Settings > General. Then use the WordPress function date_i18n() to show the local timezone. For example:

    $date_time = rwmb_meta( 'datetime' );
    echo date_i18n( 'F d, Y H:i', $date_time );
    #29038
    vubai
    Participant

    Hi,

    so the issue here is that the value stored in the database is a WordPress "timestamp + offset" not a true Unix timestamp as reported in the Meta Box docs and the former are being phased out from WordPress core as they're causing issues as reported here:
    https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/

    Also note that date_i18n() is now deprecated in favor of wp_date() as also reported in the article above.

    The example code you pasted above works as long as you're not using standard PHP date functions that expect a true Unix timestamp otherwise the time zone will be messed up.
    Expanding on your example here you can see that the output of wp_date() is wrong if you have non-UTC time zone in your WordPress settings:

    
    $date_time = rwmb_meta( 'datetime' );
    echo date_i18n( 'F d, Y H:i', $date_time );
    echo wp_date( 'F d, Y H:i', $date_time );
    

    Hope this make my point more clear.

    #29058
    Long Nguyen
    Moderator

    Hi vubai,

    Thank you for your feedback.

    Let me explain this case with the datetime field. The datetime field uses our JS library to get the current time of your computer and generate it to the timestamp value then the PHP code helps to save this value to the database.

    That means the Unix timestamp saved is the "timezone (UTC) + offset". If you use the function wp_date(), it will display "timezone (UTC) + offset" + offset (timezone in general settings). So, to display the true timezone when using the datetime field and the function wp_date(), you should use this code

    echo wp_date( 'F d, Y H:i', $date_time, new DateTimeZone('UTC') );

    or use the date() function.

    Get more details here https://developer.wordpress.org/reference/functions/wp_date/

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.