Utility Functions
The page tries to validate any date entry against the dates of the blog posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | function avp_test_dates( $year, $month = NULL, $date = NULL ) { global $wpdb; // Get first post date $query = $wpdb->prepare( “SELECT min(post_date) FROM {$wpdb->posts} WHERE post_status = ‘publish’ AND post_type = ‘post’ ”, “” ); list( $first->year, $first->month, $first->date ) = explode( ‘-’, date( “Y‑m-d”, strtotime( $wpdb->get_var( $query ) ) ) ); // Get latest date $query = $wpdb->prepare( “SELECT max(post_date) FROM {$wpdb->posts} WHERE post_status = ‘publish’ AND post_type = ‘post’ ”, “” ); list( $last->year, $last->month, $last->date ) = explode( ‘-’, date( “Y‑m-d”, strtotime( $wpdb->get_var( $query ) ) ) ); if ( ( $year < $first->year ) || ( $year > $last->year ) ) { echo ‘<p>Invalid year value ’ . $year . ‘: value outside range of published posts.</p>’; return( false ); } if ( is_null( $month ) ) return( true ); else { if ( ( 1 > $month ) || ( 12 < $month ) ) { echo ‘<p>Invalid month value ’ . $month . ‘: value outside valid range.</p>’; return( false ); } else if ( ( $year === $first->year ) && ( $month < $first->month ) ) { echo ‘<p>Invalid date value ’ . $year . ‘/’ . $month . ‘: value outside range of published posts.</p>’; return( false ); } else if ( ( $year === $last->year ) && ( $month > $last->month ) ) { echo ‘<p>Invalid date value ’ . $year . ‘/’ . $month . ‘: value outside range of published posts.</p>’; return( false ); } } if ( is_null( $date ) ) { return( true ); } else { if ( ( $year === $first->year ) && ( $month === $first->month ) && ( $date < $first->date ) ) { echo ‘<p>Invalid date value ’ . $year . ‘/’ . $month . ‘/’ . $date . ‘: value outside range of published posts.</p>’; return( false ); } else if ( ( $year === $last->year ) && ( $month === $last->month ) && ( $date > $first->date ) ) { echo ‘<p>Invalid date value ’ . $year . ‘/’ . $month . ‘/’ . $date . ‘: value outside range of published posts.</p>’; return( false ); } } return( true ); } |
The function queries the database for the oldest and newest blog posts and compares the given date. If the entry is outside the date range, the query is flagged as invalid.
This page relies on the blog posts being in the ‘year/month/date/postname’ format.
The short URL of the present article is: http://www.terryobrien.me/KmGVq
Personal Links