note 87676 added to function.checkdate
| From: | bmauseratgmail at osu1 dot php dot net | Date: | Tue, 16 Dec 2008 14:24:42 +0000 |
| Subject: | note 87676 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148214@lists.php.net to get a copy of this message | ||
Here is function that checks date if matches given format and validity of the date.
<?php
/**
* Checks date if matches given format and validity of the date.
* Examples:
* <code>
* is_date('22.22.2222', 'mm.dd.yyyy'); // returns false
* is_date('11/30/2008', 'mm/dd/yyyy'); // returns true
* is_date('30-01-2008', 'dd-mm-yyyy'); // returns true
* is_date('2008 01 30', 'yyyy mm dd'); // returns true
* </code>
* @param string $value the variable being evaluated.
* @param string $format Format of the date. Any combination of <i>mm<i>,
<i>dd<i>, <i>yyyy<i>
* with single character separator between.
*/
function is_date($value, $format = 'mm.dd.yyyy'){
if(strlen($value) == 10 && strlen($format) == 10){
// find separator. Remove all other characters from $format
$separator_only = str_replace(array('m','d','y'),'',
$format);
$separator = $separator_only[0]; // separator is first character
if($separator && strlen($separator_only) == 2){
// make regex
$regexp = str_replace('mm', '[0-1][0-9]', $value);
$regexp = str_replace('dd', '[0-3][0-9]', $value);
$regexp = str_replace('yyyy', '[0-9]{4}', $value);
$regexp = str_replace($separator, "\\" . $separator, $value);
if($regexp != $value && preg_match('/'.$regexp.'/', $value)){
// check date
$day = substr($value,strpos($format, 'd'),2);
$month = substr($value,strpos($format, 'm'),2);
$year = substr($value,strpos($format, 'y'),4);
if(@checkdate($month, $day, $year))
return true;
}
}
}
return false;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 161.53.12.135
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/87676
Del: integrated -- https://master.php.net/note/delete/87676/integrated
Del: useless -- https://master.php.net/note/delete/87676/useless
Del: bad code -- https://master.php.net/note/delete/87676/bad+code
Del: spam -- https://master.php.net/note/delete/87676/spam
Del: non-english -- https://master.php.net/note/delete/87676/non-english
Del: in docs -- https://master.php.net/note/delete/87676/in+docs
Del: other reasons-- https://master.php.net/note/delete/87676
Reject -- https://master.php.net/note/reject/87676
Search -- https://master.php.net/manage/user-notes.php