Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active June 11, 2019 16:41
Show Gist options
  • Save lenivene/b19dc9f7c22b3b63b290321db77bc19e to your computer and use it in GitHub Desktop.
Save lenivene/b19dc9f7c22b3b63b290321db77bc19e to your computer and use it in GitHub Desktop.
Function PHP - Validate Date, returns TRUE if the date given is valid; otherwise returns FALSE.
<?php
/**
* Validate Date
*
* @return boolean Returns TRUE if the date given is valid; otherwise returns FALSE.
*/
function is_date_valid( $date, $format = 'Y-m-d H:i:s'){
if( ! isset( $date ) || isset( $date ) && ! is_string( $date ) )
return false;
$date_time = DateTime::createFromFormat( $format, $date );
$is_invalid = (bool) ( $date_time && $date_time->format( $format ) == $date );
return $is_invalid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment