PHP uses Strtotime to get last month, next month, month's date, Strtotime last month _php tutorial

Source: Internet
Author: User
Tags echo date

PHP uses Strtotime to get last month, next month, month's date, Strtotime last month


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:

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, another day is 2014-02-28, It's so convenient to use date () and Strtotime ().

Get last month Date:

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 needs to get a timestamp, and then in the month-1 OK, super smart Date () will 2014-0-1 this thing converted into 2013-12-01, too cool.

Get next month's date:

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.

Finally, the usage of Strtotime is briefly introduced.

Gets the UNIX timestamp for the specified date

Strtotime ("2009-1-22") examples are as follows:
Echo strtotime ("2009-1-22")
Results: 1232553600
Description: Return January 22, 2009 0:0 0 seconds timestamp

Get English text date time

Examples are as follows:
Easy to compare, use date to convert time stamp to system time with specified timestamp

(1) Print the timestamp for tomorrow at this time Strtotime ("+1 Day")
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Result: 2009-01-22 09:40:25
Specify time:
echo Date ("Y-m-d h:i:s", Strtotime ("+1 Day")
Result: 2009-01-23 09:40:25

(2) Print the timestamp at this time yesterday Strtotime ("-1 day")
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Result: 2009-01-22 09:40:25
Specify time:
echo Date ("Y-m-d h:i:s", Strtotime ("1 day"))
Result: 2009-01-21 09:40:25

(3) Print timestamp strtotime ("+1 Week") at this time next week
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Result: 2009-01-22 09:40:25
Specify time:
echo Date ("Y-m-d h:i:s", Strtotime ("+1 Week"))
Result: 2009-01-29 09:40:25

(4) Print timestamp strtotime ("1 week") at this time last week
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Result: 2009-01-22 09:40:25
Specify time:
echo Date ("Y-m-d h:i:s", Strtotime ("1 week"))
Result: 2009-01-15 09:40:25

(5) Print the time stamp specified next week strtotime ("next Thursday")
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Result: 2009-01-22 09:40:25
Specify time:
echo Date ("Y-m-d h:i:s", Strtotime ("next Thursday"))
Result: 2009-01-29 00:00:00

(6) Print the timestamp strtotime ("last Thursday") specified on the previous day of the week
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Result: 2009-01-22 09:40:25
Specify time:
echo Date ("Y-m-d h:i:s", Strtotime ("last Thursday"))
Result: 2009-01-15 00:00:00

The above example shows that Strtotime can parse the datetime description of any English text into a Unix timestamp, and we get the specified timestamp by combining mktime () or date () format date time to achieve the required date time.

Articles you may be interested in:

    • PHP strtotime functions in a detailed
    • PHP in-depth understanding of the use of Strtotime functions
    • PHP timestamp Strtotime () using methods and tricks
    • PHP uses the Strtotime and date functions to determine if a date is a valid code share
    • Get the date of last month, next month, month (strtotime,date) under PHP
    • PHP uses the date and Strtotime functions to output a specified date method

http://www.bkjia.com/PHPjc/1086641.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086641.html techarticle PHP uses Strtotime to get last month, next month, this month's date, Strtotime wrote the program today, when suddenly found a long time ago written to get the number of months of the function, the classic ...

  • 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.