PHP obtains the strtotime (), date (), and strtotimedate_PHP tutorials for the last month, next month, and this month.

Source: Internet
Author: User
PHP obtains the date (strtotime (), date (), and strtotimedate of the previous month, next month, and this month. PHP obtains the date (strtotime (), date () of the previous month, next month, and this month, suddenly I found a function that was written a long time ago to get the number of days of the month. The classic PHP gets the date of the previous month, the next month, the date of the current month (strtotime (), date (), strtotimedate

When I was writing a program today, I suddenly found a function that was written a long time ago to get the number of days of the month. The classic switch version, but when I got the number of days of the previous month, I only set the month to 1, it is estimated that it was too difficult at the time, and then I saw a sense of treasure. I wanted to deal with it again, but I was sure there were some very convenient methods. so I found the following version, A small modification was made.

  Get the date of this month:

1 function getMonth($date){2     $firstday = date("Y-m-01",strtotime($date));3     $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));4     return array($firstday,$lastday);5 }

$ Firstday is the first day of the month. if $ date is like 2014-2, $ firstday will be. Then, add a month based on $ firstday to, and then subtract one day from, it is really convenient to use date () and strtotime.

  Get Last Month Date:

1 function getlastMonthDays($date){2     $timestamp=strtotime($date);3     $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));4     $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));5     return array($firstday,$lastday);6 }

You need to get a timestamp for the last month, and then-1 in the month will be OK. the super intelligent date () will convert 2014-0-1 to, which is so nice.

  Obtain the date of next month:

 1 function getNextMonthDays($date){ 2     $timestamp=strtotime($date); 3     $arr=getdate($timestamp); 4     if($arr['mon'] == 12){ 5         $year=$arr['year'] +1; 6         $month=$arr['mon'] -11; 7         $firstday=$year.'-0'.$month.'-01'; 8         $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day")); 9     }else{10         $firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));11         $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));12     }13     return array($firstday,$lastday);14 }

The code for the next month's date looks a little longer, because date () cannot be converted into something similar to 2014-13-01, and it will return directly to 1970, so we need to deal with the issue of December before, in addition to December, the month + 1 is OK.

In general, it is very convenient. the date function is too powerful.

Substring (strtotime (), date (), strtotimedate when writing a program today, suddenly found a function written a long time ago to get the number of days of the month, classic...

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.