How to configure timezone for timestamp in datetime field?
- This topic has 5 replies, 3 voices, and was last updated 3 years, 10 months ago by
Long Nguyen.
-
AuthorPosts
-
June 7, 2021 at 6:02 PM #28753
Bomes
ParticipantI 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 ));
June 7, 2021 at 6:37 PM #28754Bomes
ParticipantI exchanged "Europe/Berlin" by date_default_timezone_get() - made it work for me though is not 100% correct.
June 16, 2021 at 6:08 PM #28941vubai
ParticipantI'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 underjs_options
are treated as strings. There is currently no way of specifying a custom JavaScript function in the field settings.June 18, 2021 at 9:42 AM #28979Long Nguyen
ModeratorHi,
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 );
June 21, 2021 at 6:04 PM #29038vubai
ParticipantHi,
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 ofwp_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 ofwp_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.
June 22, 2021 at 2:38 PM #29058Long Nguyen
ModeratorHi vubai,
Thank you for your feedback.
Let me explain this case with the
datetime
field. Thedatetime
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 thedatetime
field and the functionwp_date()
, you should use this codeecho 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/
-
AuthorPosts
- You must be logged in to reply to this topic.