note 94071 modified in function.date by thiago
| From: | thiago@php.net | Date: | Thu, 15 Oct 2009 10:51:07 +0000 |
| Subject: | note 94071 modified in function.date by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-161761@lists.php.net to get a copy of this message | ||
1. Find whether an year is a leap year or not.
2. Find the number of days in a given month of a year.
<?php
function leapYear($year){
if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 != 0)) return TRUE;
return FALSE;
}
function daysInMonth($month = 0, $year = ''){
$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$d = array("Jan" => 31, "Feb" => 28, "Mar" => 31,
"Apr" => 30, "May" => 31, "Jun" => 30, "Jul" =>
31, "Aug" => 31, "Sept" => 30, "Oct" => 31, "Nov"
=> 30, "Dec" => 31);
if(!is_numeric($year) || strlen($year) != 4) $year = date('Y');
if($month == 2 || $month == 'Feb'){
if(leapYear($year)) return 29;
}
if(is_numeric($month)){
if($month < 1 || $month > 12) return 0;
else return $days_in_month[$month - 1];
}
else{
if(in_array($month, array_keys($d))) return $d[$month];
else return 0;
}
}
?>
You can call the function as <?php daysInMonth(02, 2008); ?> or <?php
daysInMonth('Feb', 2008); ?>
If no year is specified then the current year will be used/shown.
--was--
1. Find whether an year is a leap year or not.
2. Find the number of days in a given month of a year.
code -
function leapYear($year){
if ($year % 400 == 0 || ($year % 4 == 0 && $year % 100 != 0)) return TRUE;
return FALSE;
}
function daysInMonth($month = 0, $year = ''){
$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$d = array("Jan" => 31, "Feb" => 28, "Mar" => 31,
"Apr" => 30, "May" => 31, "Jun" => 30, "Jul" =>
31, "Aug" => 31, "Sept" => 30, "Oct" => 31, "Nov"
=> 30, "Dec" => 31);
if(!is_numeric($year) || strlen($year) != 4) $year = date('Y');
if($month == 2 || $month == 'Feb'){
if(leapYear($year)) return 29;
}
if(is_numeric($month)){
if($month < 1 || $month > 12) return 0;
else return $days_in_month[$month - 1];
}
else{
if(in_array($month, array_keys($d))) return $d[$month];
else return 0;
}
}
You can call the function as daysInMonth(02, 2008); or daysInMonth('Feb', 2008);
If no year is specified then the current year will be used/shown.
http://php.net/manual/en/function.date.php