PHP gets the number of days of the month and the date array based on the month

Source: Internet
Author: User
This article mainly introduces the method by which PHP obtains the number of days of the month and the date array according to the date, related to the relevant judgment, conversion and the traversal operation of the character and array of PHP, and the need of friends can refer to the following

Specific as follows:

function Get_day ($date)  {    $tem = explode ('-', $date);//Cut date get year and month    $year = $tem [' 0 '];    $month = $tem [' 1 '];    if (In_array ($month, Array (1, 3, 5, 7, 8, (,),, (), ())    {      //$text = $year. '. $mont H. ' Month has 31 days ';      $text = ' + ';    }    ElseIf ($month = = 2)    {      if ($year%400 = = 0 | | ($year%4 = = 0 && $year%100!== 0))    Determines whether a leap        year {//$text = $year. '. $month. ' The month has 29 days ';        $text = ' $ ';      }      else{        //$text = $year. ' $month. ' The month has 28 days ';        $text = ' + ';      }    }    else{      //$text = $year. ' $month. ' The month has 30 days ';      $text = ' a ';    }    return $text;} echo get_day (' 2016-8-1 ');

Operation Result: 31

Transform, return an array of dates:

/*** gets the number of days in the month * @param $date * @param $rtype 1 days 2 specific date array * @return */function get_day ($date, $rtype = ' 1 ')  {    $tem = E Xplode ('-', $date);    The cut date gets the year and month    $year = $tem [' 0 '];    $month = $tem [' 1 '];    if (In_array ($month, Array (1, 3, 5, 7, 8, (,),, (), ())    {      //$text = $year. '. $mont H. ' Month has 31 days ';      $text = ' + ';    }    ElseIf ($month = = 2)    {      if ($year%400 = = 0 | | ($year%4 = = 0 && $year%100!== 0))    Determines whether a leap        year {//$text = $year. '. $month. ' The month has 29 days ';        $text = ' $ ';      }      else{        //$text = $year. ' $month. ' The month has 28 days ';        $text = ' + ';      }    }    else{      //$text = $year. ' $month. ' The month has 30 days ';      $text = ' a ';    }    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 '));

The results of the operation are as follows:

Array (+) {[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)" 20 16-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]=> stri Ng (9) "2016-8-16" [16]=> string (9) "2016-8-17" [17]=> string (9) "2016-8-18" [18]=> string (9) "2016-8-19" [19]=&G T 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]=> string (9)" 2016-8-28 "[28]=> string (9)" 2016-8-29 "[29]=> string (9)" 2016-8-30 "[30]=> string (9)" 2 016-8-31 "}

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.