wppaste
WordPress

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
Builds a MySQL format date/time based on some query parameters.

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
32–123

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 174.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
!is_array($datetime) && preg_match() && $dt === false32preg_match(), wp_timezone(), date_create(), gmdate()
!is_array($datetime) && preg_match() && $dt !== false34preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format()
is_array($datetime) && isset($datetime)37–61array_map(), sprintf()
!is_array($datetime) && $dt === false41preg_match(), preg_match(), wp_timezone(), date_create(), gmdate()
is_array($datetime) && !isset($datetime)42–66array_map(), current_time(), sprintf()
!is_array($datetime) && $dt !== false43preg_match(), preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format()
!is_array($datetime) && !preg_match() && $dt === false45–61preg_match(), preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), gmdate()
!is_array($datetime) && !preg_match() && $dt !== false47–63preg_match(), preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format()
preg_match() && isset($datetime)50–74preg_match(), array_map(), sprintf()
!is_array($datetime) && $dt === false50preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), gmdate()
!is_array($datetime) && $dt !== false52preg_match(), preg_match(), preg_match(), wp_timezone(), date_create(), ->setTimezone(), ->format()
preg_match() && !isset($datetime)55–79preg_match(), array_map(), current_time(), sprintf()
16 further outcomes, up to 112 instructions
is_array($datetime)56–76array_map(), mktime(), gmdate(), sprintf()
isset($datetime)59–83preg_match(), preg_match(), array_map(), sprintf()
is_array($datetime) && !isset($datetime)61–81array_map(), current_time(), mktime(), gmdate(), sprintf()
!preg_match() && isset($datetime)63–103preg_match(), preg_match(), preg_match(), preg_match(), array_map(), sprintf()
!isset($datetime)64–88preg_match(), preg_match(), array_map(), current_time(), sprintf()
isset($datetime)68–92preg_match(), preg_match(), preg_match(), array_map(), sprintf()
!preg_match() && !isset($datetime)68–108preg_match(), preg_match(), preg_match(), preg_match(), array_map(), current_time(), sprintf()
preg_match()69–89preg_match(), array_map(), mktime(), gmdate(), sprintf()
!isset($datetime)73–97preg_match(), preg_match(), preg_match(), array_map(), current_time(), sprintf()
preg_match() && !isset($datetime)74–94preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf()
always78–98preg_match(), preg_match(), array_map(), mktime(), gmdate(), sprintf()
!preg_match()82–118preg_match(), preg_match(), preg_match(), preg_match(), array_map(), mktime(), gmdate(), sprintf()
!isset($datetime)83–103preg_match(), preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf()
always87–107preg_match(), preg_match(), preg_match(), array_map(), mktime(), gmdate(), sprintf()
!preg_match() && !isset($datetime)87–123preg_match(), preg_match(), preg_match(), preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf()
!isset($datetime)92–112preg_match(), preg_match(), preg_match(), array_map(), current_time(), mktime(), gmdate(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17432–12318
8.517432–12318
8.417432–12318
8.317432–12318
8.217432–12318
8.117432–123181 fewer instruction than PHP 7.4
7.417532–12418

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.