note 53592 deleted from function.mktime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:36:11 +0000 |
| Subject: | note 53592 deleted from function.mktime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142077@lists.php.net to get a copy of this message | ||
Note Submitter: Hayden
----
Here is a quick way to check the a date is valid. I use this when I get users to enter dates via
drop downs, which leave open the possibility for impossible dates like 30-Feb-2005 and 31-Jun-2005
etc. This is only necessary as mktime() will accept these invalid dates, which is, in my case,
sometimes not the desired handling.
<?php
function isvaliddate($day, $month, $year) {
$day = intval($day);
$month = intval($month);
$year = intval($year);
$time = mktime(0,0,0,$month,$day,$year);
if ($day != date("j",$time)) return false;
if ($month != date("n",$time)) return false;
if ($year != date("Y",$time)) return false;
return true;
}
?>
Note that date("Y",$time) checks the year as a 4 digit year. That line could be replaced
as shown below to allow for the 2 digit notation as well.
Also the order the arguments need to be specified is day, month, year, because I come from NZ and
that is the format we use here. If you prefer another format, for example month, day, year, just
change the order of the arguments on the first line.
<?php
function isvaliddate($day, $month, $year) {
$day = intval($day);
$month = intval($month);
$year = intval($year);
$time = mktime(0,0,0,$month,$day,$year);
if ($day != date("j",$time)) return false;
if ($month != date("n",$time)) return false;
if (($year != date("Y",$time)) AND ($year != date("y",$time))) return false;
return true;
}
?>
Hope this helps someone!
Cheers,
Hayden.