note 53592 added to function.mktime
| From: | Hayden at osu1 dot php dot net | Date: | Tue, 07 Jun 2005 00:33:59 +0000 |
| Subject: | note 53592 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90029@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53592
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53592&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53592&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53592&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53592&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53592&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53592&report=yes
Search -- http://master.php.net/manage/user-notes.php