WP_Date_Query::validate_date_values( array $date_query = array() ): bool
- Since
- 4.1.0
- Source
wp-includes/class-wp-date-query.php:285
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
- Scaling
- Scales with input
- Instructions
- 41–160
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 284.
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 it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($date_query) && !$date_query && !isset($_year) | 41–71 | none |
!empty($date_query) && !$date_query | 54–88 | mktime(), gmdate() |
!empty($date_query) && !$date_query && !isset($_year) && !empty($day_month_year_error_msg) | 59–76 | _doing_it_wrong() |
!empty($date_query) && !isset($_year) | 63–93 | reset(), mktime(), gmdate() |
!empty($date_query) && isset($_year) | 71–101 | mktime(), gmdate(), mktime(), gmdate() |
!empty($date_query) && !$date_query && !empty($day_month_year_error_msg) | 72–93 | mktime(), gmdate(), _doing_it_wrong() |
!empty($date_query) && !$date_query && !isset($_year) && wp_checkdate() && empty($day_month_year_error_msg) | 74–90 | wp_checkdate() |
!empty($date_query) && isset($_year) | 76–103 | reset(), mktime(), gmdate(), mktime(), gmdate() |
!empty($date_query) && !$date_query && !isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg) | 79–95 | wp_checkdate(), _doing_it_wrong() |
!empty($date_query) && !isset($_year) && !empty($day_month_year_error_msg) | 81–98 | reset(), mktime(), gmdate(), _doing_it_wrong() |
!empty($date_query) && !$date_query && wp_checkdate() && empty($day_month_year_error_msg) | 87–107 | mktime(), gmdate(), wp_checkdate() |
!empty($date_query) && isset($_year) && !empty($day_month_year_error_msg) | 89–106 | mktime(), gmdate(), mktime(), gmdate(), _doing_it_wrong() |
28 further outcomes, up to 160 instructions
!empty($date_query) && !$date_query && wp_checkdate() && !empty($day_month_year_error_msg) | 92–112 | mktime(), gmdate(), wp_checkdate(), _doing_it_wrong() |
!empty($date_query) && isset($_year) && !empty($day_month_year_error_msg) | 94–108 | reset(), mktime(), gmdate(), mktime(), gmdate(), _doing_it_wrong() |
!empty($date_query) && !isset($_year) && wp_checkdate() && empty($day_month_year_error_msg) | 96–112 | reset(), mktime(), gmdate(), wp_checkdate() |
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg) | 98–112 | wp_checkdate(), __(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && !isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg) | 101–117 | reset(), mktime(), gmdate(), wp_checkdate(), _doing_it_wrong() |
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg) | 103–117 | wp_checkdate(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong() |
!empty($date_query) && isset($_year) && wp_checkdate() && empty($day_month_year_error_msg) | 104–120 | mktime(), gmdate(), mktime(), gmdate(), wp_checkdate() |
!empty($date_query) && isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg) | 109–125 | mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), _doing_it_wrong() |
!empty($date_query) && isset($_year) && wp_checkdate() && empty($day_month_year_error_msg) | 109–122 | reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate() |
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg) | 110–123 | wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && !$date_query && !wp_checkdate() && empty($day_month_year_error_msg) | 111–129 | mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && isset($_year) && wp_checkdate() && !empty($day_month_year_error_msg) | 114–127 | reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), _doing_it_wrong() |
!empty($date_query) && !$date_query && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg) | 115–128 | wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf(), _doing_it_wrong() |
!empty($date_query) && !$date_query && !wp_checkdate() && !empty($day_month_year_error_msg) | 116–134 | 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) | 120–134 | reset(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && !$date_query && !wp_checkdate() && empty($day_month_year_error_msg) | 123–140 | mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg) | 125–139 | reset(), 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–145 | 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) | 128–142 | mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && !isset($_year) && !wp_checkdate() && empty($day_month_year_error_msg) | 132–145 | reset(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg) | 133–147 | 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) | 133–144 | reset(), mktime(), gmdate(), mktime(), gmdate(), wp_checkdate(), __(), esc_html(), esc_html(), sprintf() |
!empty($date_query) && !isset($_year) && !wp_checkdate() && !empty($day_month_year_error_msg) | 137–150 | reset(), 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) | 138–149 | reset(), 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) | 140–153 | 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) | 145–158 | mktime(), 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) | 145–155 | reset(), 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) | 150–160 | reset(), 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 284 | 41–160 | 28 | |
| 8.5 | 284 | 41–160 | 28 | |
| 8.4 | 284 | 41–160 | 28 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 294 | 41–167 | 28 | |
| 8.2 | 294 | 41–167 | 28 | |
| 8.1 | 294 | 41–167 | 28 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 296 | 41–167 | 28 |
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
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- _doing_it_wrong()Marks something as being incorrectly called.
- wp_checkdate()Tests if the supplied date is valid for the Gregorian calendar.
- WP_Date_Query::validate_date_values()Validates the given date_query values and triggers errors if something is not valid.
Used by · 2
- WP_Date_Query::sanitize_query()Recursive-friendly query sanitizer.
- WP_Date_Query::validate_date_values()Validates the given date_query values and triggers errors if something is not valid.
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 = 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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.