Get the date of last month, next month, Month in PHP

Source: Internet
Author: User

  Because the work needs to obtain the last month, next month, this month's date, special from the website to find the implementation code, special sharing, easy to need friends

Today, when I write the program, suddenly found a long time ago to get the number of days of the function, the classic switch version, but to get the number of days last month, I just put the month-1, I guess it was too sleepy, and then see a kind of creepy feeling, would like to deal with, But I think there are some super convenient way, so I found the following version, made a little change.   Get this month's date:   code is as follows: function getmonth ($date) {      $firstday = Date ("y-m-01", Strtotime ($date)); &N Bsp     $lastday = Date ("y-m-d", Strtotime ("$firstday +1 month-1 Day"));      return Array ($firstday, $lastday); &NBSP}     $firstday is the first day of the month, if $date is 2014-2 this way, $firstday will be 2014-02-01, then according to $firstday plus one months is 2014-03-01, Another day is 2014-02-28, with date () and strtotime () really convenient.   Get last month:     code as follows: function Getlastmonthdays ($date) {      $timestamp =strtotime ($date); &n Bsp     $firstday =date (' y-m-01 ', Strtotime) (Date (' Y ', $timestamp). ' -'. (Date (' m ', $timestamp)-1). ' -01 '));       $lastday =date (' y-m-d ', Strtotime ("$firstday +1 month-1 Day"));      return Array ($firstday, $lastday); &NBSP}     Last month date you need to get a timestamp and then on the month-1 is OK, the Super smart Date () will convert 2014-0-1 this thing to 2013-12-01, it's so cool.   Get next month Date:   Code is as follows: function Getnextmonthdays ($date) {    $timestamp =strtotime ($date);     $ Arr=getdate ($timestamp);     if ($arr [' mon '] = =) {        $year = $arr [' year '] +1;         $month = $arr [' mon ']-11;         $firstday = $year. '-0 '. $month. '-01 ';         $lastday =date (' y-m-d ', Strtotime ("$firstday +1 month-1 Day"));    }else{        $firstday =date (' y-m-01 ', strtotime (date (' Y ', $timestamp). ' -'. (Date (' m ', $timestamp) +1). ' -01 '));         $lastday =date (' y-m-d ', Strtotime ("$firstday +1 month-1 Day"));    }     return array ($firstday, $lastday); The code for the     next month's date looks a little bit longer because date () is not going to be like 2014-13-01, it will go straight back to 1970, so you need to deal with the December problem before, except for December, the month +1 is OK.   Always, it's very convenient, the date function is too powerful.  

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.