How to get the previous month and year relative to today using strtotime and date?

I need to get the previous month and year relative to the current date.

However, see the following example.

// Today is 2011-03-30 echo date('Ym-d', strtotime('last month')); // Output: 2011-03-02 

This behavior is understandable (to a certain point) due to the different number of days in February and March, and the code in the above example is what I need, but it only works 100% correctly between the 1st and 28th of each month.

So, how to get the last month AND year (think of date("Ym") ) in the most elegant way that works every day of the year? The optimal solution will be based on the analysis of strtotime arguments.

Update To clarify the requirements a bit.

I have a piece of code that receives statistics for the last few months, but first I show statistics for the last month, and then load the other months when necessary. This is the intended goal. So, during this month I want to find out which month-year I have to pull in order to download the PREVIOUS monthly statistics.

I also have code that is related to the time zone (this is not really important right now), and accepts the strtotime string as input (to initialize the internal date), and then allows the date / time to be adjusted, also using strtotime -connected strings.

I know that this can be done with a few conditional expressions and basic math, but it is really messy compared to this, for example (if it works correctly, of course):

 echo tz::date('last month')->format('Y-d') 

So, I ONLY need the previous month and year, in a strtotime compatible way.

Answer (thanks, @dnagirl):

 // Today is 2011-03-30 echo date('Ym-d', strtotime('first day of last month')); // Output: 2011-02-01 
+57
date php strtotime
Mar 30 2018-11-11T00:
source share
14 answers

Take a look at the DateTime class. It must do the calculations correctly, and the date formats are compatible with strttotime . Something like:

 $datestring='2011-03-30 first day of last month'; $dt=date_create($datestring); echo $dt->format('Y-m'); //2011-02 
+48
Mar 30 '11 at 17:35
source share

if the day itself does not matter, do the following:

 echo date('Ym-d', strtotime(date('Y-m')." -1 month")); 
+28
Mar 30 '11 at 17:08
source share

I found the answer, because today I had the same problem as the 31st. This is not a bug in php , as some suggested, but expected functionality (in some of them). According to this post , what strtotime really does is set one month ago by one and does not change the number of days. Thus, in the case of May 31, he is looking for April 31, which is an invalid date. So he takes April 30th, and then adds 1 day past him and gives May 1st.

In your example, 2011-03-30 it will return from one month until February 30, which is not valid from February, only for 28 days. Then he takes the difference of these days (30-28 = 2), and then moves two days after February 28, which is March 2.

As others have pointed out, the best way to get "last month" is to add either "first day" or "last day" using either strtotime or a DateTime object:

 // Today being 2012-05-31 //All the following return 2012-04-30 echo date('Ym-d', strtotime("last day of -1 month")); echo date('Ym-d', strtotime("last day of last month")); echo date_create("last day of -1 month")->format('Ym-d'); // All the following return 2012-04-01 echo date('Ym-d', strtotime("first day of -1 month")); echo date('Ym-d', strtotime("first day of last month")); echo date_create("first day of -1 month")->format('Ym-d'); 

Thus, using them, you can create a date range if you make a request, etc.

+23
May 31 '12 at 16:27
source share

If you want the previous year and month to relate to a specific date and have access to DateTime, you can do this:

 $d = new DateTime('2013-01-01', new DateTimeZone('UTC')); $d->modify('first day of previous month'); $year = $d->format('Y'); //2012 $month = $d->format('m'); //12 
+9
Nov 12 '13 at 9:27
source share
 date('Y-m', strtotime('first day of last month')); 
+7
Jul 31 '14 at 8:34
source share

strtotime have a second parameter, timestamp , which makes the first parameter relative to the second parameter. So you can do this:

 date('Y-m', strtotime('-1 month', time())) 
+6
Aug 26 '16 at 9:57
source share

if I correctly understood the question that you need only in the last month and the year in which it is:

 <?php $month = date('m'); $year = date('Y'); $last_month = $month-1%12; echo ($last_month==0?($year-1):$year)."-".($last_month==0?'12':$last_month); ?> 

Here is an example: http://codepad.org/c99nVKG8

+5
Mar 30 '11 at 17:05
source share

ehh, this is not a mistake, as one person is mentioned. this is the expected behavior, as the number of days in a month is often different. The easiest way to get the previous month using strtotime is probably to use -1 month from the first of this month.

 $date_string = date('Y-m', strtotime('-1 month', strtotime(date('Ym-01')))); 
+4
Mar 30 '11 at 17:21
source share

I think you found an error in the strtotime function. Whenever I have to get by, I am always in mathematics by the value of the month / year. Try something like this:

 $LastMonth = (date('n') - 1) % 12; $Year = date('Y') - !$LastMonth; 
+2
Mar 30 '11 at 17:06
source share

Maybe a little longer than you want, but I used more code than maybe too long for it to be more readable.

However, it comes out with the same result as you - what do you want / expect it to come out?

 //Today is whenever I want it to be. $today = mktime(0,0,0,3,31,2011); $hour = date("H",$today); $minute = date("i",$today); $second = date("s",$today); $month = date("m",$today); $day = date("d",$today); $year = date("Y",$today); echo "Today: ".date('Ym-d', $today)."<br/>"; echo "Recalulated: ".date("Ymd",mktime($hour,$minute,$second,$month-1,$day,$year)); 

If you just want the month and year, then just set the day to “01” and not “day”:

  $day = 1; 

This should give you what you need. You can simply set the hour, minute and second to zero, and you are not interested in using them either.

  date("Ym",mktime(0,0,0,$month-1,1,$year); 

Shortens it a bit ,-)

+1
Mar 30 '11 at 17:13
source share

This is due to the fact that the previous month has fewer days than the current month. I fixed this by first checking to see if the previous month has fewer days than the current month and changing the calculation based on this.

If he has fewer days, then get the last day -1 month to get the current day -1 month:

 if (date('d') > date('d', strtotime('last day of -1 month'))) { $first_end = date('Ym-d', strtotime('last day of -1 month')); } else { $first_end = date('Ym-d', strtotime('-1 month')); } 
+1
May 31 '12 at 12:31
source share

If the DateTime solution is acceptable, this snippet returns the year of the last month and the month of the last month, avoiding the possible trap when you start it in January.

 function fn_LastMonthYearNumber() { $now = new DateTime(); $lastMonth = $now->sub(new DateInterval('P1M')); $lm= $lastMonth->format('m'); $ly= $lastMonth->format('Y'); return array($lm,$ly); } 
0
Nov 11 '16 at 11:37
source share
 //return timestamp, use to format month, year as per requirement function getMonthYear($beforeMonth = '') { if($beforeMonth !="" && $beforeMonth >= 1) { $date = date('Y')."-".date('m')."-15"; $timestamp_before = strtotime( $date . ' -'.$beforeMonth.' month' ); return $timestamp_before; } else { $time= time(); return $time; } } //call function $month_year = date("Ym",getMonthYear(1));// last month before current month $month_year = date("Ym",getMonthYear(2)); // second last month before current month 
0
Jan 07 '18 at 4:17
source share
 function getOnemonthBefore($date){ $day = intval(date("t", strtotime("$date")));//get the last day of the month $month_date = date("ymd",strtotime("$date -$day days"));//get the day 1 month before return $month_date; } 

The resulting date depends on the number of days that make up the input month. If the input month is February (28 days), 28 days before February 5 - January 8. If the entry can be 17, 31 days before April 16. Similarly, if the entry can be 31, the final date will be April 30th.

NOTE : the input accepts the full date ("ymd") and outputs ("ymd"), you can change this code to suit your needs.

0
Feb 01 '18 at 10:20
source share



All Articles