PHP uses Strtotime to get last month, next month, month date _php tips

Source: Internet
Author: User
Tags current time echo date

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:

 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 $date is 2014-2 so, $firstday will be 2014-02-01, and then according to $firstday plus one months is 2014-03-01, minus one day is 2014-02-28, It's really convenient to use date () and Strtotime ().

Get date of last month:

 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 on the month-1 is OK, super smart Date () will convert 2014-0-1 this thing to 2013-12-01, it's cool.

Get Next month 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 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, except for the December direct month +1 is OK.

Generally speaking, it is very convenient, the date function is too powerful.

Finally, briefly introduce the usage of Strtotime

Gets the UNIX timestamp for the specified date

The Strtotime ("2009-1-22") example is as follows:
Echo strtotime ("2009-1-22")
Results: 1232553600
Note: Return January 22, 2009 0:0 0 seconds time stamp

Get English text date time

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

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

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

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

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

(5) Print a timestamp strtotime ("next Thursday") that specifies the next day of the week
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
echo Date ("Y-m-d h:i:s", Strtotime ("next Thursday")
Results: 2009-01-29 00:00:00

(6) Print a timestamp strtotime ("last Thursday") that specifies the previous week
Current time:
echo Date ("Y-m-d h:i:s", Time ())
Results: 2009-01-22 09:40:25
Specified time:
echo Date ("Y-m-d h:i:s", Strtotime ("last Thursday")
Results: 2009-01-15 00:00:00

The above example shows that Strtotime can resolve the date-time description of any English text to a Unix timestamp, and we combine the mktime () or date () format date time to get the specified timestamp to achieve the required DateTime.

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.