A method that displays each month for a period of time returns an array of those months

  • 2020-05-17 04:51:08
  • OfStack

 
/** 
*  Generates an array of months from the start month to the end month  
*  The method is modeled after dang zihao getDateArr() methods  
* @param unknown_type $start 
* @param unknown_type $end 
*/ 
function getMonthArr($start, $end) 
{ 
$start = empty($start) ? date('Y-m',strtotime('-1 month')) : $start; 
$end = empty($end) ? date('Y-m') : $end; 

// Convert to timestamp  
$st = strtotime($start.'-01'); 
$et = strtotime($end.'-01'); 

$t = $st; 
$i = 0; 
while($t <= $et) 
{ 
// So let's add up the total number of seconds per month   Calculation formula: top 1 month 1 The number of timestamp seconds of the current month minus the number of timestamp seconds of the current month  
// I don't understand why I want to go  
$d[$i] = trim(date('Y-m',$t),' '); 
$t += strtotime('+1 month', $t)-$t; 
$i++; 
} 
return $d; 
} 

Related articles: