位置:  首页 -> 编程语言 -> PHP -> PHP根据年月获取当月天数

PHP根据年月获取当月天数

判断时间是否存在

if($_GET["date"]){
		$date=explode("-",$_GET["date"]);
		$year = $date["0"];
		$month = $date["1"];
	}else{
		$year = date('Y',time());
		$month = date('m',time());
	}
如果你需要按月份调出数据,就需要判断参数是否存在,不存在则默认为当月

函数代码

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;
    }   

月份为 1 3 5 7 8 10 12,则为 31天,为  4 6 9 11,则是 30天

二月需另作闰年判断,普通年能被四整除且不能被100整除的为闰年,世纪年能被400整除的是闰年.(如2000年是闰年,1900年不是闰年)


到底了 2

相关阅读