php根據(jù)年月獲取當(dāng)月天數(shù)及日期數(shù)組的方法
本文實(shí)例講述了php根據(jù)年月獲取當(dāng)月天數(shù)及日期數(shù)組的方法。分享給大家供大家參考,具體如下:
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');
運(yùn)行結(jié)果為:31
改造,返回日期數(shù)組:
/** * 獲取當(dāng)月天數(shù) * @param $date * @param $rtype 1天數(shù) 2具體日期數(shù)組 * @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'));
運(yùn)行結(jié)果如下:
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" }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php日期與時(shí)間用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP連接MSSQL時(shí)nvarchar字段長(zhǎng)度被截?cái)酁?55的解決方法
這篇文章主要介紹了PHP連接MSSQL時(shí)nvarchar字段長(zhǎng)度被截?cái)酁?55的解決方法,可通過(guò)CAST數(shù)據(jù)類型轉(zhuǎn)換來(lái)解決,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12php設(shè)計(jì)模式 Template (模板模式)
定義一個(gè)操作中的算法骨架,而將一些步驟延遲到子類中,使得子類可以不改變一個(gè)算法的結(jié)構(gòu)可以定義該算法的某些特定步驟2011-06-06PHP中strpos、strstr和stripos、stristr函數(shù)分析
這篇文章主要為大家詳細(xì)介紹了PHP中strpos、strstr和stripos、stristr函數(shù)源碼,感興趣的朋友可以參考一下2016-06-06

PHP排序之二維數(shù)組的按照字母排序?qū)崿F(xiàn)代碼

php HtmlReplace輸入過(guò)濾安全函數(shù)

深入理解PHP之?dāng)?shù)組(遍歷順序) Laruence原創(chuàng)

php簡(jiǎn)單實(shí)現(xiàn)sql防注入的方法