note 41964 modified in function.cal-days-in-month by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:53:00 +0000 |
| Subject: | note 41964 modified in function.cal-days-in-month by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79940@lists.php.net to get a copy of this message | ||
How about this one:
<?php
function daysinmonth($month, $year)
{
$days=31;
while(!checkdate($month, $days, $year)) $days--;
return $days;
}
?>
Not exactly a one-line solution, but easy to follow, not limited to 1970-2038 (but don't use it
after the year 32767) and uses a built-in function that's always available.
Be sure that $month and $year are valid, or the loop will run forever. Possibly add:
&& $days>0
to the while condition. This would return 0 as your error code.
--was--
How about this one:
function daysinmonth($month, $year)
{
$days=31;
while(!checkdate($month, $days, $year)) $days--;
return $days;
}
Not exactly a one-line solution, but easy to follow, not limited to 1970-2038 (but don't use it
after the year 32767) and uses a built-in function that's always available.
Be sure that $month and $year are valid, or the loop will run forever. Possibly add:
&& $days>0
to the while condition. This would return 0 as your error code.
http://php.net/manual/en/function.cal-days-in-month.php