wppaste
WordPress

WP_Date_Query::validate_date_values( array $date_query = array() ): bool

Since
4.1.0
Source
wp-includes/class-wp-date-query.php:285
Validates the given date_query values and triggers errors if something is not valid.

Description

Note that date queries with invalid date ranges are allowed to continue (though of course no items will be found for impossible dates).
This method only generates debug notices for these cases.

Compatibility

WordPress
since 4.1.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

$date_queryarrayoptional
The date_query array.Default: array()

Return value

bool
True if all values in the query are valid, false if one or more fail.

Performance profile

How much work a call to WP_Date_Query::validate_date_values() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
41–161

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Date_Query::validate_date_values()

Further down the call graph this can also reach option, 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 · 40 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Date_Query::validate_date_values() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($date_query) && !$date_query && !isset($_year)41–71none
!empty($date_query) && !$date_query54–89mktime(), gmdate()
!empty($date_query) && !$date_query && !isset($_year) && !empty($day_month_year_error_msg)59–76_doing_it_wrong()
!empty($date_query) && !isset($_year)64–94reset(), mktime(), gmdate()
!empty($date_query) && !$date_query && !empty($day_month_year_error_msg)72–94mktime(), gmdate(), _doing_it_wrong()
!empty($date_query) && isset($_year)72–102mktime(), gmdate(), mktime(), gmdate()
!empty($date_query) && !$date_query && !isset($_year) && wp_checkdate() && empty($day_month_year_error_msg)74–90wp_checkdate()
!empty($date_query) && isset($_year)77–104reset(), mktime(), gmdate(), mktime(), gmdate()
!empty($date_query) && !$date_query && !isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg)79–95wp_checkdate(), _doing_it_wrong()
!empty($date_query) && !isset($_year) && !empty($day_month_year_error_msg)82–99reset(), mktime(), gmdate(), _doing_it_wrong()
!empty($date_query) && !$date_query && wp_checkdate() && empty($day_month_year_error_msg)87–108mktime(), gmdate(), wp_checkdate()
!empty($date_query) && isset($_year) && !empty($day_month_year_error_msg)90–107mktime(), gmdate(), mktime(), gmdate(), _doing_it_wrong()
28 further outcomes, up to 161 instructions
!empty($date_query) && !$date_query && wp_checkdate() && !empty($day_month_year_error_msg)92–113mktime(), gmdate(), wp_checkdate(), _doing_it_wrong()
!empty($date_query) && isset($_year) && !empty($day_month_year_error_msg)95–109reset(), mktime(), gmdate(), mktime(), gmdate(), _doing_it_wrong()
!empty($date_query) && !isset($_year) && wp_checkdate() && empty($day_month_year_error_msg)97–113reset(), mktime(), gmdate(), wp_checkdate()
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)98–112wp_checkdate(), __(), esc_html(), esc_html(), sprintf()
!empty($date_query) && !isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg)102–118reset(), mktime(), gmdate(), wp_checkdate(), _doing_it_wrong()
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)103–117wp_checkdate(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && wp_checkdate() && empty($day_month_year_error_msg)105–121mktime(), gmdate(), mktime(), gmdate(), wp_checkdate()
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)110–123wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf()
!empty($date_query) && isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg)110–126mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), _doing_it_wrong()
!empty($date_query) && isset($_year) && wp_checkdate() && empty($day_month_year_error_msg)110–123reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate()
!empty($date_query) && !$date_query && !wp_checkdate() && empty($day_month_year_error_msg)111–130mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf()
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)115–128wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg)115–128reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), _doing_it_wrong()
!empty($date_query) && !$date_query && !wp_checkdate() && !empty($day_month_year_error_msg)116–135mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)121–135reset(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf()
!empty($date_query) && !$date_query && !wp_checkdate() && empty($day_month_year_error_msg)123–141mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf()
!empty($date_query) && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)126–140reset(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && !$date_query && !wp_checkdate() && !empty($day_month_year_error_msg)128–146mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)129–143mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf()
!empty($date_query) && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)133–146reset(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf()
!empty($date_query) && isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)134–148mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)134–145reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf()
!empty($date_query) && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)138–151reset(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)139–150reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)141–154mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf()
!empty($date_query) && isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)146–159mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
!empty($date_query) && isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg)146–156reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf()
!empty($date_query) && isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg)151–161reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev28541–16128
8.528541–16128
8.428541–1612810 fewer instructions than PHP 8.3
8.329541–16828
8.229541–16828
8.129541–168282 fewer instructions than PHP 7.4
7.429741–16828

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 · 5

Used by · 2

Source code

	public function validate_date_values( $date_query = array() ) {		if ( empty( $date_query ) ) {			return false;		} 		$valid = true; 		/*		 * Validate 'before' and 'after' up front, then let the		 * validation routine continue to be sure that all invalid		 * values generate errors too.		 */		if ( array_key_exists( 'before', $date_query ) && is_array( $date_query['before'] ) ) {			$valid = $this->validate_date_values( $date_query['before'] );		} 		if ( array_key_exists( 'after', $date_query ) && is_array( $date_query['after'] ) ) {			$valid = $this->validate_date_values( $date_query['after'] );		} 		// Array containing all min-max checks.		$min_max_checks = array(); 		// Days per year.		if ( array_key_exists( 'year', $date_query ) ) {			/*			 * If a year exists in the date query, we can use it to get the days.			 * If multiple years are provided (as in a BETWEEN), use the first one.			 */			if ( is_array( $date_query['year'] ) ) {				$_year = reset( $date_query['year'] );			} else {				$_year = $date_query['year'];			} 			$max_days_of_year = (int) gmdate( 'z', mktime( 0, 0, 0, 12, 31, $_year ) ) + 1;		} else {			// Otherwise we use the max of 366 (leap-year).			$max_days_of_year = 366;		} 		$min_max_checks['dayofyear'] = array(			'min' => 1,			'max' => $max_days_of_year,		); 		// Days per week.		$min_max_checks['dayofweek'] = array(			'min' => 1,			'max' => 7,		); 		// Days per week.		$min_max_checks['dayofweek_iso'] = array(			'min' => 1,			'max' => 7,		); 		// Months per year.		$min_max_checks['month'] = array(			'min' => 1,			'max' => 12,		); 		// Weeks per year.		if ( isset( $_year ) ) {			/*			 * If we have a specific year, use it to calculate number of weeks.			 * Note: the number of weeks in a year is the date in which Dec 28 appears.			 */			$week_count = gmdate( 'W', mktime( 0, 0, 0, 12, 28, $_year ) ); 		} else {			// Otherwise set the week-count to a maximum of 53.			$week_count = 53;		} 		$min_max_checks['week'] = array(			'min' => 1,			'max' => $week_count,

Changelog

Introduced in 4.1.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 6.9.7 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.