On this page
rest_get_date_with_gmt( string $date, bool $is_utc = false ): array|null
Parses a date into both its local and UTC equivalent, in MySQL datetime format.
Description
See also
Parameters
$datestring Required-
RFC3339 timestamp.
$is_utcbool Optional-
Whether the provided date should be interpreted as UTC.
Default:
false
Return
array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s), null on failure.
Source
File: wp-includes/rest-api.php. View all references
function rest_get_date_with_gmt( $date, $is_utc = false ) {
/*
* Whether or not the original date actually has a timezone string
* changes the way we need to do timezone conversion.
* Store this info before parsing the date, and use it later.
*/
$has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
$date = rest_parse_date( $date );
if ( empty( $date ) ) {
return null;
}
/*
* At this point $date could either be a local date (if we were passed
* a *local* date without a timezone offset) or a UTC date (otherwise).
* Timezone conversion needs to be handled differently between these two cases.
*/
if ( ! $is_utc && ! $has_timezone ) {
$local = gmdate( 'Y-m-d H:i:s', $date );
$utc = get_gmt_from_date( $local );
} else {
$utc = gmdate( 'Y-m-d H:i:s', $date );
$local = get_date_from_gmt( $utc );
}
return array( $local, $utc );
}
Related
Uses
| Uses | Description |
|---|---|
| rest_parse_date() wp-includes/rest-api.php | Parses an RFC3339 time into a Unix timestamp. |
| get_gmt_from_date() wp-includes/formatting.php | Given a date in the timezone of the site, returns that date in UTC. |
| get_date_from_gmt() wp-includes/formatting.php | Given a date in UTC or GMT timezone, returns that date in the timezone of the site. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Posts_Controller::prepare_item_for_database() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post for create or update. |
| WP_REST_Comments_Controller::prepare_item_for_database() wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php | Prepares a single comment to be inserted into the database. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_get_date_with_gmt