wp_checkdate()
Test if the supplied date is valid for the Gregorian calendar.
1 time — 0.00002 sec (very fast) | 50000 times — 0.10 sec (speed of light)
Hooks from the function
Return
true|false
. True if valid date, false if not valid date.
Usage
wp_checkdate( $month, $day, $year, $source_date );
- $month(int) (required)
- Month number.
- $day(int) (required)
- Day number.
- $year(int) (required)
- Year number.
- $source_date(string) (required)
- The date to filter.
Examples
#1 An example from the WordPress core
/* * If the date is not set ( new post or draft) * if it is not a draft or the date was changed in the process of writing. */ if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) { $post_date = current_time( 'mysql' ); } else { $post_date = $postarr['post_date']; } // Date Validation. $mm = substr( $post_date, 5, 2 ); $jj = substr( $post_date, 8, 2 ); $aa = substr( $post_date, 0, 4 ); $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); if ( ! $valid_date ) { if ( $wp_error ) { //The specified date is incorrect. return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) ); } else { return 0; } }
#2 Another example from the WordPress core
if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) { $year = get_query_var( 'year' ); $month = get_query_var( 'monthnum' ); $day = get_query_var( 'day' ); $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); if ( ! wp_checkdate( $month, $day, $year, $date ) ) { $redirect_url = get_month_link( $year, $month ); } }
Changelog
Since 3.5.0 | Introduced. |
wp_checkdate() wp checkdate code WP 6.8
function wp_checkdate( $month, $day, $year, $source_date ) { /** * Filters whether the given date is valid for the Gregorian calendar. * * @since 3.5.0 * * @param bool $checkdate Whether the given date is valid. * @param string $source_date Date to check. */ return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date ); }