在项目中做数据统计时,为了使数据准确,我们需要精准地获取当月天数
判断时间是否存在
如果你需要按月份调出数据,就需要判断参数是否存在,不存在则默认为当月
if($_GET["date"]){
$date=explode("-",$_GET["date"]);
$year = $date["0"];
$month = $date["1"];
}else{
$year = date('Y',time());
$month = date('m',time());
}
系统函数
$time='2020-2'
date('t', strtotime($time));
// 29
自写函数
月份为 1 3 5 7 8 10 12,则为 31天,为 4 6 9 11,则是 30天
二月需另作闰年判断,普通年能被四整除且不能被100整除的为闰年,世纪年能被400整除的是闰年.(如2000年是闰年,1900年不是闰年)
function get_day( $year,$month ) {
if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
{
$text = '31';
}elseif( $month == 2 )
{
if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年
{
$text = '29';
}
else{
$text = '28';
}
}else{
$text = '30';
}
return $text;
}