rest_get_date_with_gmt( string $date, bool $is_utc = false ): array|null
- Since
- 4.4.0
- Source
wp-includes/rest-api.php:1389
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$datestring- RFC3339 timestamp.
$is_utcbooloptional- Whether the provided date should be interpreted as UTC. Default false.Default:
false
Return value
array|null- Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s), null on failure.
$0stringLocal datetime string.$1stringUTC datetime string.
Performance profile
How much work a call to rest_get_date_with_gmt() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 10–24
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 34.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_get_date_with_gmt() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$date === false | 10 | rest_parse_date() |
$date !== false | 22–23 | rest_parse_date(), gmdate(), get_date_from_gmt() |
$date !== false && !$date | 24 | rest_parse_date(), gmdate(), get_gmt_from_date() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 34 | 10–24 | 3 | |
| 8.5 | 34 | 10–24 | 3 | |
| 8.4 | 34 | 10–24 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 37 | 13–27 | 3 | |
| 8.2 | 37 | 13–27 | 3 | |
| 8.1 | 37 | 13–27 | 3 | |
| 7.4 | 37 | 13–27 | 3 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- rest_parse_date()Parses an RFC3339 time into a Unix timestamp.
- get_gmt_from_date()Given a date in the timezone of the site, returns that date in UTC.
- get_date_from_gmt()Given a date in UTC or GMT timezone, returns that date in the timezone of the site.
Used by · 2
- WP_REST_Comments_Controller::prepare_item_for_database()Prepares a single comment to be inserted into the database.
- WP_REST_Posts_Controller::prepare_item_for_database()Prepares a single post for create or update.
Source code
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 ( false === $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 );}Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.