Created
May 31, 2020 18:22
-
-
Save mmuoDev/1a036dc1abc9640f83be528361727d75 to your computer and use it in GitHub Desktop.
Validate date and time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateDateTime($value){ | |
$array = explode(" ", $value); | |
if(count($array) != 2){ | |
return 'Kindly separate your date and time in this format for example, 11/12/2020 2.20pm'; | |
} | |
//validate date | |
$date = $array[0]; | |
if(preg_match("/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/", $date) === 0) { | |
return 'Your date '.$date.' is in an invalid format'; | |
} | |
//validate | |
$time = $array[1]; | |
if(preg_match("/^(0?[1-9]|1[0-2]):[0-5][0-9](?:am|pm)$/", $time) === 0) { | |
return 'Your time '.$time.' is in an invalid format'; | |
} | |
return true; | |
} | |
$value = '20-12-2000 13:30pm'; | |
validateDateTime($value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment