#58192 closed defect (bug) (invalid)
get_post_datetime() always returns local time even when using 'gmt' as $source
| Reported by: | nboole | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Date/Time | Version: | 6.2 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: |
Description
This code (below) is copied directly from the github repo at https://github.com/WordPress/wordpress-develop/blob/6.2/src/wp-includes/general-template.php#L2793-L2821
<?php function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) { $post = get_post( $post ); if ( ! $post ) { return false; } $wp_timezone = wp_timezone(); if ( 'gmt' === $source ) { $time = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt; $timezone = new DateTimeZone( 'UTC' ); } else { $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date; $timezone = $wp_timezone; } if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) { return false; } $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone ); if ( false === $datetime ) { return false; } return $datetime->setTimezone( $wp_timezone ); }
In the function above, the variable $wp_timezone is created with the following line: $wp_timezone = wp_timezone(); and it is never modified in the function, which means that $wp_timezone is always the LOCAL timezone for the site.
In the if( 'gmt' === $source) part of the function, the variable $timezone is set based on whether or not the user asked for 'gmt' or 'local' time in the $source parameter.
However, in the return statement, DateTimeImmutable::setTimezone() is called with the variable $wp_timezone rather than using the correctly set variable, $timezone.
This means that get_post_datetime($post, 'date', 'gmt') still returns the WP Local time rather than GMT/UTC time as intended.
The return statement should be re-written to: return $datetime->setTimezone( $timezone );
Change History (6)
This ticket was mentioned in PR #4377 on WordPress/wordpress-develop by eidolonrage.
4 years ago
#1
- Keywords has-patch added
This ticket was mentioned in PR #4378 on WordPress/wordpress-develop by eidolonrage.
4 years ago
#2
Function changed: get_post_datetime()
Changes made:
Changed function's return statement from return $datetime->setTimezone( $wp_timezone ); to return $datetime->setTimezone( $timezone );
Reasoning for change:
The variable $wp_timezone is a timezone object which is always set to the site's local time. As such, the function get_post_datetime previously ignored the $source parameter, returning site local time even if the $source param was set to 'gmt'
With the change made, the $timezone variable, which is correctly set to either local time or gmt time based on the $source param, should now be returned. That means that get_post_datetime should now correctly take the $source param into account and return the GMT/UTC time when requested by the user.
Trac ticket: https://core.trac.wordpress.org/ticket/58192
This ticket was mentioned in PR #4379 on WordPress/wordpress-develop by @saumilgajjar.
4 years ago
#3
…'gmt' as $source
return the gmt/local timezone
Trac ticket:
#4
@
4 years ago
- Milestone Awaiting Review
- Resolution → invalid
- Status new → closed
Return in local time is intended and documented behavior for the function. There are options for input to instantiate from, but the output is always given in a consistent state and not meant to be configured (this reduces complexity of arguments and room for error). If UTC time is needed, it can be derived from the resulting object.
@SergeyBiryukov commented on PR #4378:
4 years ago
#5
Thanks for the PR! Closing for now as per https://core.trac.wordpress.org/ticket/58192#comment:4.
@SergeyBiryukov commented on PR #4379:
4 years ago
#6
Thanks for the PR! Closing for now as per https://core.trac.wordpress.org/ticket/58192#comment:4.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Changes made to src/wp-includes/general-template.php:
changed return variable in get_post_datetime() from $wp_timezone (which is a timezone object always set to local site time) to $timezone (which is a timezone object set to either GMT/UTC or Local depending on the user's $source parameter)
This should allow the user to request the post_datetime in either local time or gmt time, as intended/expected according to the function's description and params.
Trac ticket: https://core.trac.wordpress.org/ticket/58192