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

Source: Internet
Author: User
Because the job needs to get last month, next month, this month's date, special from the website found the implementation code, special sharing, easy to use friends

Write the program today, suddenly found a long time to write a long time 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, it was too sleepy, then saw a kind of creepy feeling, would like to deal with again, But I think there is something super convenient way, so found the following version, made a little change. Get this month's date: The code is as follows: function GetMonth ($date) {$firstday = date ("y-m-01", Strtotime ($date)), $lastday = Date ("y-m-d", Strtotime ( "$firstday +1 month-1 Day"); Return Array ($firstday, $lastday); } $firstday is the first day of the month, if the year is 2014-2, then the $firstday will be 2014-02-01, then according to the $firstday plus one months is 2014-03-01, minus one day is 2014-02-28,   It's so convenient to use date () and Strtotime (). Get last month Date: Code as follows: function Getlastmonthdays ($date) {$timestamp =strtotime ($date), $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);   Last month's date requires a timestamp, then 1 on the month, and the Super Smart Date () will convert 2014-0-1 to 2013-12-01. Get next month's date: code 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 bit longer because date () doesn't go like this 2014-13-01, it will go straight back to 1970, so we need to deal with the problem of December, except for December the direct month +1 is OK. Generally speaking, it is very convenient, the date function is too powerful.

http://www.bkjia.com/PHPjc/730217.html www.bkjia.com true http://www.bkjia.com/PHPjc/730217.html techarticle because the work needs to obtain last month, next month, this month's date, special from the website found the implementation code, special sharing, convenient for the needs of friends today to write programs, suddenly found ...

  • Related Article

    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.