PHP如何判斷某年的某月有多少天

來源:互聯網
上載者:User
最近做一個結算系統,需要在月底自動進行結算,那麼首先得判斷當天是不是一個月的最後一天,演算法很簡單,貼出來算是自己做筆記了,本文主要和大家分享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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.