Get last month's date range

I need help getting the full date range for previous months in the following format: Ymd

I managed to get the full date range of the "this" months, but not the full date range of the "previous" months.

Any help is much appreciated!

+4
source share
4 answers
$last_month_first_day=strtotime('first day of last month'); $no_of_days=date('t',$last_month_first_day); $date_value=$last_month_first_day; for($i=0;$i<$no_of_days;$i++) { echo date('Ym-d',$date_value)."<br>"; $date_value=strtotime("+1 day",$date_value); } 

This code will print what you want.

First Date:

 echo date('Ym-d',strtotime('first day of last month')); 

Last date:

 echo date('Ym-d',strtotime('last day of last month')); 
+7
source

This task performs correctly:

 echo date('Ym-01 - Ym-t', strtotime('previous month')); 

Here is the proof: http://ideone.com/L82ZW

+10
source

You can do something like this:

 $month = 2; $lastday = mktime(0, 0, 0, $month+1, 0, 2012); $firstday = mktime(0, 0, 0, $month, 1, 2012); $end = date("Ymd", $lastday); $start = date("Ymd", $firstday); 

The last day of any month can be expressed as "0" the day of the next month.
http://www.php.net/manual/en/function.mktime.php

+2
source
 // works with PHP 5.3 or later $today = new DateTime(); $thisMonthFirstDay = $today->setDate($today->format('Y'), $today->format('m'), 1); $previousMonthLastDay = $thisMonthFirstDay->sub(new DateInterval('P1D')); // substract 1 day $daysInLastMonth = $previousMonthLastDay->format('d'); for($i=1; $i<=$daysInLastMonth; $i++) { $num = ($i < 10) ?'0'.$i :$i; // add zero in front if < 10 echo $previousMonthLastDay->format('Ym-') . $num. "\n"; } 
0
source

Source: https://habr.com/ru/post/1388827/


All Articles