note 106916 modified in function.checkdate by danbrown
| From: | danbrown@php.net | Date: | Tue, 03 Jan 2012 22:09:01 +0000 |
| Subject: | note 106916 modified in function.checkdate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185323@lists.php.net to get a copy of this message | ||
don't use strtotime() to format inputs to checkdate(). It would give false results.
Eg,
Below function would return true though it should have been false
<?php
$str = '2011-11-31'; //invalid date, november has 30 days only
function isDateValid($str)
{
$stamp = strtotime($str);
if (!is_numeric($stamp))
return FALSE;
//checkdate(month, day, year)
if ( checkdate(date('m', $stamp), date('d', $stamp), date('Y',
$stamp)) )
{
return TRUE;
}
return FALSE;
}
?>
The reason is that strtotime() coverts 2011-11-31 to 2011-12-01.
check with date('Y-m-d', strtotime('2011-11-31'))
--was--
don't use strtotime() to format inputs to checkdate(). It would give false results.
Eg,
Below function would return true though it should have been false
$str = '2011-11-31'; //invalid date, november has 30 days only
function isDateValid($str)
{
$stamp = strtotime($str);
if (!is_numeric($stamp))
return FALSE;
//checkdate(month, day, year)
if ( checkdate(date('m', $stamp), date('d', $stamp), date('Y',
$stamp)) )
{
return TRUE;
}
return FALSE;
}
The reason is that strtotime() coverts 2011-11-31 to 2011-12-01.
check with date('Y-m-d', strtotime('2011-11-31'))
http://php.net/manual/en/function.checkdate.php