上邊是純算術(shù)算出來的,當(dāng)然還有更簡單的方法:
/**
* 判斷某年的某月有多少天
* @return [type] [description]
*/
function daysInmonth($year='',$month=''){
if(empty($year)) $year = date('Y');
if(empty($month)) $month = date('m');
if (in_array($month, array(1, 3, 5, 7, 8, '01', '03', '05', '07', '08', 10, 12))) {
$text = '31'; //月大
}elseif ($month == 2 || $month == '02'){
if ( ($year % 400 == 0) || ( ($year % 4 == 0) && ($year % 100 !== 0) ) ) { //判斷是否是閏年
$text = '29'; //閏年2月
} else {
$text = '28'; //平年2月
}
} else {
$text = '30'; //月小
}
return $text;
}
/**
* 判斷某年的某月有多少天
* @return [type] [description]
*/
function daysInmonth1($year='',$month=''){
if(empty($year)) $year = date('Y');
if(empty($month)) $month = date('m');
$day = '01';
//檢測日期是否合法
if(!checkdate($month,$day,$year)) return '輸入的時(shí)間有誤';
//獲取當(dāng)年當(dāng)月第一天的時(shí)間戳(時(shí),分,秒,月,日,年)
$timestamp = mktime(0,0,0,$month,$day,$year);
$result = date('t',$timestamp);
return $result;
}
$d=cal_days_in_month(CAL_GREGORIAN,10,2005);
echo("There was $d <br><br>");
$i=2;
$y=2013;
echo date("t",strtotime("$y-$i"));
function get_day( $date )
{
$tem = explode('/' , $date); //切割日期 得到年份和月份
$year = $tem['0'];
$month = $tem['1'];
if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
{
$text = $year.'年的'.$month.'月有31天';
}
elseif( $month == 2 )
{
if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判斷是否是閏年
{
$text = $year.'年的'.$month.'月有29天';
}
else{
$text = $year.'年的'.$month.'月有28天';
}
}
else{
$text = $year.'年的'.$month.'月有30天';
}
return $text;
}
$i=2;
$y=2013;
echo get_day($y.'/'.$i);
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇