国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php根據年月獲取當月天數及日期數組的方法

2024-05-04 23:53:03
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 莱州市| 财经| 太白县| 闽侯县| 尼玛县| 环江| 塔河县| 车险| 芦溪县| 西吉县| 寿宁县| 西畴县| 商水县| 西乌| 新田县| 镇安县| 昌宁县| 西城区| 大名县| 阿勒泰市| 尚志市| 宜昌市| 安福县| 泸水县| 四川省| 锡林郭勒盟| 固原市| 宕昌县| 赣州市| 武城县| 海丰县| 吴江市| 邵武市| 珲春市| 江川县| 宝山区| 徐水县| 吉林省| 海林市| 神木县| 博乐市|