使用php怎么根据年月获取当月的天数

发布时间:2021-03-19 16:49:14 作者:Leah
来源:亿速云 阅读:242

本篇文章为大家展示了使用php怎么根据年月获取当月的天数,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

具体如下:

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天';
      $text = '31';
    }
    elseif( $month == 2 )
    {
      if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) )    //判断是否是闰年
      {
        // $text = $year.'年的'.$month.'月有29天';
        $text = '29';
      }
      else{
        // $text = $year.'年的'.$month.'月有28天';
        $text = '28';
      }
    }
    else{
      // $text = $year.'年的'.$month.'月有30天';
      $text = '30';
    }
    return $text;
}
echo get_day('2016-8-1');

运行结果为:31

改造,返回日期数组:

/**
* 获取当月天数
* @param $date 
* @param $rtype 1天数 2具体日期数组
* @return 
*/
function get_day( $date ,$rtype = '1')  
{
    $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天';
      $text = '31';
    }
    elseif( $month == 2 )
    {
      if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) )    //判断是否是闰年
      {
        // $text = $year.'年的'.$month.'月有29天';
        $text = '29';
      }
      else{
        // $text = $year.'年的'.$month.'月有28天';
        $text = '28';
      }
    }
    else{
      // $text = $year.'年的'.$month.'月有30天';
      $text = '30';
    }
    if ($rtype == '2') {
      for ($i = 1; $i <= $text ; $i ++ ) {
        $r[] = $year."-".$month."-".$i;
      }
    } else {
      $r = $text;
    }
    return $r;
}
var_dump(get_day('2016-8-1','2'));

运行结果如下:

array(31) {
 [0]=>
 string(8) "2016-8-1"
 [1]=>
 string(8) "2016-8-2"
 [2]=>
 string(8) "2016-8-3"
 [3]=>
 string(8) "2016-8-4"
 [4]=>
 string(8) "2016-8-5"
 [5]=>
 string(8) "2016-8-6"
 [6]=>
 string(8) "2016-8-7"
 [7]=>
 string(8) "2016-8-8"
 [8]=>
 string(8) "2016-8-9"
 [9]=>
 string(9) "2016-8-10"
 [10]=>
 string(9) "2016-8-11"
 [11]=>
 string(9) "2016-8-12"
 [12]=>
 string(9) "2016-8-13"
 [13]=>
 string(9) "2016-8-14"
 [14]=>
 string(9) "2016-8-15"
 [15]=>
 string(9) "2016-8-16"
 [16]=>
 string(9) "2016-8-17"
 [17]=>
 string(9) "2016-8-18"
 [18]=>
 string(9) "2016-8-19"
 [19]=>
 string(9) "2016-8-20"
 [20]=>
 string(9) "2016-8-21"
 [21]=>
 string(9) "2016-8-22"
 [22]=>
 string(9) "2016-8-23"
 [23]=>
 string(9) "2016-8-24"
 [24]=>
 string(9) "2016-8-25"
 [25]=>
 string(9) "2016-8-26"
 [26]=>
 string(9) "2016-8-27"
 [27]=>
 string(9) "2016-8-28"
 [28]=>
 string(9) "2016-8-29"
 [29]=>
 string(9) "2016-8-30"
 [30]=>
 string(9) "2016-8-31"
}

上述内容就是使用php怎么根据年月获取当月的天数,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. PHP获取2个时间点之间的年月
  2. Javascript获取某个月的天数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:怎么在PHP中利用数组生成一个XML格式的数据

下一篇:怎么在PHP中根据key对二维数组进行分组

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》