《PHP教程:php根据年月获取当月天数及日期数组的方法》要点:
本文介绍了PHP教程:php根据年月获取当月天数及日期数组的方法,希望对您有用。如果有疑问,可以联系我们。
本文实例讲述了php根据年月获取当月天数及日期数组的方法.分享给大家供大家参考,具体如下: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');
运行结果为:31PHP实例
改造,返回日期数组:PHP实例
/*** 获取当月天数* @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'));
运行结果如下:PHP实例
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"}
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, LeCMS, ClassCMS, Fastadmin, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。