Created
May 4, 2013 06:19
-
-
Save reggiegutter/5516450 to your computer and use it in GitHub Desktop.
Regular Expression (Regex) to validate date and the format of the date (dd/mm/yyyy).
I use the regex to check if the data provided is in the required format. Then I use the PHP function checkdate($dd, $mm, $yyyy) to check if the values provided are right. For example, the function will return FALSE if the user provide a date like 30/02/2013, or …
This file contains hidden or 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
<?php | |
// function to check dates | |
public function date_check($date) | |
{ | |
$exp_regular = '/^(\d{2})[\/](\d{2})[\/](\d{4})/'; | |
$ret = preg_match($exp_regular, $date, $matches); | |
if($ret === 1) | |
{ | |
$dd = $matches[1]; | |
$mm = $matches[2]; | |
$yyyy = $matches[3]; | |
if (checkdate($mm, $dd, $yyyy)) | |
{ | |
echo "The date is correct"; | |
return TRUE; | |
} | |
else | |
{ | |
echo "The date is wrong"; | |
return FALSE; | |
} | |
} | |
else | |
{ | |
echo "The date must have this format: dd/mm/yyyy"; | |
return FALSE; | |
} | |
} | |
// Hope it helps | |
// Any problem, report then in the comments. | |
// Thanks! | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment