WP_Date_Query::build_mysql_datetime( string|array $datetime, bool $default_to_max = false ): string|false
- Since
- 3.7.0
- Source
wp-includes/class-wp-date-query.php:882
Description
You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can pass a string that will be passed to date_create().
Compatibility
- WordPress
- since 3.7.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
$datetimestring|array- An array of parameters or a strtotime() string.
$default_to_maxbooloptional- Whether to round up incomplete dates. Supported by values of $datetime that are arrays, or string values that are a subset of MySQL date format ('Y', 'Y-m', 'Y-m-d', 'Y-m-d H:i').
Default: false.Default:false
Return value
string|false- A MySQL format date/time or false on failure.
Performance profile
How much work a call to WP_Date_Query::build_mysql_datetime() 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
- Moderate
- Scaling
- Constant
- Instructions
- 32–123
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 174.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()one call below WP_Date_Query::build_mysql_datetime()
Further down the call graph this can also reach hook, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 28 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Date_Query::build_mysql_datetime() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_array($datetime) && preg_match() && $dt === false | 32 | preg_match(), wp_timezone(), date_create(), gmdate() |
!is_array($datetime) && preg_match() && $dt !== false | 34 | preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format() |
is_array($datetime) && isset($datetime) | 37–61 | array_map(), sprintf() |
!is_array($datetime) && $dt === false | 41 | preg_match(), preg_match(), wp_timezone(), date_create(), gmdate() |
is_array($datetime) && !isset($datetime) | 42–66 | array_map(), current_time(), sprintf() |
!is_array($datetime) && $dt !== false | 43 | preg_match(), preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format() |
!is_array($datetime) && !preg_match() && $dt === false | 45–61 | preg_match(), preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), gmdate() |
!is_array($datetime) && !preg_match() && $dt !== false | 47–63 | preg_match(), preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format() |
preg_match() && isset($datetime) | 50–74 | preg_match(), array_map(), sprintf() |
!is_array($datetime) && $dt === false | 50 | preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), gmdate() |
!is_array($datetime) && $dt !== false | 52 | preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format() |
preg_match() && !isset($datetime) | 55–79 | preg_match(), array_map(), current_time(), sprintf() |
16 further outcomes, up to 112 instructions
is_array($datetime) | 56–76 | array_map(), mktime(), gmdate(), sprintf() |
isset($datetime) | 59–83 | preg_match(), preg_match(), array_map(), sprintf() |
is_array($datetime) && !isset($datetime) | 61–81 | array_map(), current_time(), mktime(), gmdate(), sprintf() |
!preg_match() && isset($datetime) | 63–103 | preg_match(), preg_match(), preg_match(), preg_match(), array_map(), sprintf() |
!isset($datetime) | 64–88 | preg_match(), preg_match(), array_map(), current_time(), sprintf() |
isset($datetime) | 68–92 | preg_match(), preg_match(), preg_match(), array_map(), sprintf() |
!preg_match() && !isset($datetime) | 68–108 | preg_match(), preg_match(), preg_match(), preg_match(), array_map(), current_time(), sprintf() |
preg_match() | 69–89 | preg_match(), array_map(), mktime(), gmdate(), sprintf() |
!isset($datetime) | 73–97 | preg_match(), preg_match(), preg_match(), array_map(), current_time(), sprintf() |
preg_match() && !isset($datetime) | 74–94 | preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf() |
| always | 78–98 | preg_match(), preg_match(), array_map(), mktime(), gmdate(), sprintf() |
!preg_match() | 82–118 | preg_match(), preg_match(), preg_match(), preg_match(), array_map(), mktime(), gmdate(), sprintf() |
!isset($datetime) | 83–103 | preg_match(), preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf() |
| always | 87–107 | preg_match(), preg_match(), preg_match(), array_map(), mktime(), gmdate(), sprintf() |
!preg_match() && !isset($datetime) | 87–123 | preg_match(), preg_match(), preg_match(), preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf() |
!isset($datetime) | 92–112 | preg_match(), preg_match(), preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 174 | 32–123 | 18 | |
| 8.5 | 174 | 32–123 | 18 | |
| 8.4 | 174 | 32–123 | 18 | |
| 8.3 | 174 | 32–123 | 18 | |
| 8.2 | 174 | 32–123 | 18 | |
| 8.1 | 174 | 32–123 | 18 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 175 | 32–124 | 18 |
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 · 2
- wp_timezone()Retrieves the timezone of the site as a `DateTimeZone` object.
- current_time()Retrieves the current time based on specified type.
Used by · 1
- WP_Date_Query::get_sql_for_clause()Turns a first-order date query into SQL for a WHERE clause.
Source code
public function build_mysql_datetime( $datetime, $default_to_max = false ) { if ( ! is_array( $datetime ) ) { /* * Try to parse some common date formats, so we can detect * the level of precision and support the 'inclusive' parameter. */ if ( preg_match( '/^(\d{4})$/', $datetime, $matches ) ) { // Y $datetime = array( 'year' => (int) $matches[1], ); } elseif ( preg_match( '/^(\d{4})\-(\d{2})$/', $datetime, $matches ) ) { // Y-m $datetime = array( 'year' => (int) $matches[1], 'month' => (int) $matches[2], ); } elseif ( preg_match( '/^(\d{4})\-(\d{2})\-(\d{2})$/', $datetime, $matches ) ) { // Y-m-d $datetime = array( 'year' => (int) $matches[1], 'month' => (int) $matches[2], 'day' => (int) $matches[3], ); } elseif ( preg_match( '/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2})$/', $datetime, $matches ) ) { // Y-m-d H:i $datetime = array( 'year' => (int) $matches[1], 'month' => (int) $matches[2], 'day' => (int) $matches[3], 'hour' => (int) $matches[4], 'minute' => (int) $matches[5], ); } // If no match is found, we don't support default_to_max. if ( ! is_array( $datetime ) ) { $wp_timezone = wp_timezone(); // Assume local timezone if not provided. $dt = date_create( $datetime, $wp_timezone ); if ( false === $dt ) { return gmdate( 'Y-m-d H:i:s', false ); } return $dt->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' ); } } $datetime = array_map( 'absint', $datetime ); if ( ! isset( $datetime['year'] ) ) { $datetime['year'] = current_time( 'Y' ); } if ( ! isset( $datetime['month'] ) ) { $datetime['month'] = ( $default_to_max ) ? 12 : 1; } if ( ! isset( $datetime['day'] ) ) { $datetime['day'] = ( $default_to_max ) ? (int) gmdate( 't', mktime( 0, 0, 0, $datetime['month'], 1, $datetime['year'] ) ) : 1; } if ( ! isset( $datetime['hour'] ) ) { $datetime['hour'] = ( $default_to_max ) ? 23 : 0; } if ( ! isset( $datetime['minute'] ) ) { $datetime['minute'] = ( $default_to_max ) ? 59 : 0; } if ( ! isset( $datetime['second'] ) ) { $datetime['second'] = ( $default_to_max ) ? 59 : 0; }Changelog
Introduced in 3.7.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 7.1.0 tag, from
src/wp-includes/class-wp-date-query.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.