PHP判断某年的某月有多少天

最近做一个结算系统,需要在月底自动进行结算,那么首先得判断当天是不是一个月的最后一天,算法很简单,贴出来算是自己做笔记了^_^

/**
 * 判断某年的某月有多少天
 * @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 '输入的时间有误';
	
	//获取当年当月第一天的时间戳(时,分,秒,月,日,年)
	$timestamp = mktime(0,0,0,$month,$day,$year);
	$result = date('t',$timestamp);
	return $result;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值