note 87676 deleted from function.checkdate by danbrown
| From: | danbrown@php.net | Date: | Wed, 18 Mar 2009 13:53:37 +0000 |
| Subject: | note 87676 deleted from function.checkdate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152091@lists.php.net to get a copy of this message | ||
Note Submitter: bmauser at gmail
----
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;
}
?>