note 102539 deleted from function.checkdate by danbrown
[email protected] Sun, 20 Feb 2011 07:44:00 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
Note Submitter: waldemar dot axdorph at gmail dot com
----
amcanadian1973's script will not handle dates like 2011-02-30 i.e. false dates. This is because strtotime will convert 2011-02-30 to 2011-03-02. The script then handles the date 2011-03-02 and not the given date (2011-02-30). This is my take on it.
Please observe that this script only handles the YYYY-MM-DD format.
<?php
/*
var_dump( is_date('2011-02-20') ); // today => bool(true)
var_dump( is_date('2011-02-30') ); // non-existent => bool(false)
var_dump( is_date('2012-02-29') ); // leap day => bool(true)
var_dump( is_date('2011-2-22') ); // malformed => bool(false)
var_dump( is_date('YYYY-MM-DD') ); // characters => bool(false)
*/
function is_date( $str, $delimiter = '-' )
{
$str = trim($str);
if(strlen($str) != 10) // strlen('YYYY-MM-DD') == 10
{
return false;
}
$stamp = strtotime( $str ); //ok for first defense
if ( !is_numeric( $stamp ) )
{
return false;
}
$dateParts = explode( $delimiter, $str );//explode the date by its delimiter
$year = $dateParts[0];
$month = $dateParts[1];
$day = $dateParts[2];
if (checkdate( $month, $day, $year ))
{
return true;
}
return false;
}
?>