WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Самая быстрая Тема-конструктор для WordPress
функция не описана

WP_Date_Query::build_mysql_datetime() public WP 3.7.0

Builds a MySQL format date/time based on some query parameters.

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().

Это метод класса: WP_Date_Query{}

Хуков нет.

Возвращает

Строку/false. A MySQL format date/time or false on failure

Использование

$WP_Date_Query = new WP_Date_Query();
$WP_Date_Query->build_mysql_datetime( $datetime, $default_to_max );
$datetime(строка/массив) (обязательный)
An array of parameters or a strotime() string
$default_to_max(true/false)
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').
По умолчанию: false

Список изменений

С версии 3.7.0 Введена.

Код WP_Date_Query::build_mysql_datetime() WP 5.5.1

<?php
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' => intval( $matches[1] ),
			);

		} elseif ( preg_match( '/^(\d{4})\-(\d{2})$/', $datetime, $matches ) ) {
			// Y-m
			$datetime = array(
				'year'  => intval( $matches[1] ),
				'month' => intval( $matches[2] ),
			);

		} elseif ( preg_match( '/^(\d{4})\-(\d{2})\-(\d{2})$/', $datetime, $matches ) ) {
			// Y-m-d
			$datetime = array(
				'year'  => intval( $matches[1] ),
				'month' => intval( $matches[2] ),
				'day'   => intval( $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'   => intval( $matches[1] ),
				'month'  => intval( $matches[2] ),
				'day'    => intval( $matches[3] ),
				'hour'   => intval( $matches[4] ),
				'minute' => intval( $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;
	}

	return sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $datetime['year'], $datetime['month'], $datetime['day'], $datetime['hour'], $datetime['minute'], $datetime['second'] );
}