note 107569 added to function.checkdate
| From: | pmmmm | Date: | Fri, 17 Feb 2012 23:56:46 +0000 |
| Subject: | note 107569 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186202@lists.php.net to get a copy of this message | ||
Here's a function that further builds on top of what was posted earlier (bmauser and ystein M).
It takes date and format (D, DD, M, MM, YY, YYYY) and any seperator. Format example: YYYY-MM-DD. The
previous functions didn't take dashes and required specific order.
<?php
function valid_date($date, $format = 'YYYY-MM-DD'){
if(strlen($date) >= 8 && strlen($date) <= 10){
$separator_only = str_replace(array('M','D','Y'),'',
$format);
$separator = $separator_only[0];
if($separator){
$regexp = str_replace($separator, "\\" . $separator, $format);
$regexp = str_replace('MM', '(0[1-9]|1[0-2])', $regexp);
$regexp = str_replace('M', '(0?[1-9]|1[0-2])', $regexp);
$regexp = str_replace('DD', '(0[1-9]|[1-2][0-9]|3[0-1])', $regexp);
$regexp = str_replace('D', '(0?[1-9]|[1-2][0-9]|3[0-1])', $regexp);
$regexp = str_replace('YYYY', '\d{4}', $regexp);
$regexp = str_replace('YY', '\d{2}', $regexp);
if($regexp != $date && preg_match('/'.$regexp.'$/', $date)){
foreach (array_combine(explode($separator,$format), explode($separator,$date)) as
$key=>$value) {
if ($key == 'YY') $year = '20'.$value;
if ($key == 'YYYY') $year = $value;
if ($key[0] == 'M') $month = $value;
if ($key[0] == 'D') $day = $value;
}
if (checkdate($month,$day,$year)) return true;
}
}
}
return false;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 67.190.172.16
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/107569
Del: integrated -- https://master.php.net/note/delete/107569/integrated
Del: useless -- https://master.php.net/note/delete/107569/useless
Del: bad code -- https://master.php.net/note/delete/107569/bad+code
Del: spam -- https://master.php.net/note/delete/107569/spam
Del: non-english -- https://master.php.net/note/delete/107569/non-english
Del: in docs -- https://master.php.net/note/delete/107569/in+docs
Del: other reasons-- https://master.php.net/note/delete/107569
Reject -- https://master.php.net/note/reject/107569
Search -- https://master.php.net/manage/user-notes.php