您现在的位置: 365建站网 > 365文章 > PHP判断某年的某月有多少天(获取该月天数)

PHP判断某年的某月有多少天(获取该月天数)

文章来源:365jz.com     点击数:1715    更新时间:2018-03-31 19:18   参与评论
最近做一个结算系统,需要在月底自动进行结算,那么首先得判断当天是不是一个月的最后一天,算法很简单,贴出来算是自己做笔记了^_^


/**
* 判断某年的某月有多少天
* @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; 


另外 PHP判断某年的某月有多少天(获取该月天数) 的方法收集:

有3种方法:
1、

$d=cal_days_in_month(CAL_GREGORIAN,10,2005);
echo("There was $d <br><br>");


2、【推荐】

$i=2;
$y=2013;
echo date("t",strtotime("$y-$i"));


3、

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

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (1715人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号