Home  >  Article  >  Backend Development  >  A method that displays each month within a certain period of time and returns an array consisting of these months_PHP Tutorial

A method that displays each month within a certain period of time and returns an array consisting of these months_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:42830browse

Copy code The code is as follows:

/**
* Generate a month array from the start month to the end month
* This method is modeled after Dang Zihao’s getDateArr() method
* @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)
{
//Accumulate here The calculation formula for the total number of seconds in each month: the timestamp seconds on the 1st of the previous month minus the timestamp seconds of the current month
//I don’t understand what I want to say
$d[$i] = trim(date('Y-m',$t),' ');
$t += strtotime('+1 month', $t)-$t;
$i++;
}
return $d;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325458.htmlTechArticleCopy code The code is as follows: /** * Generate a month array from the starting month to the ending month* This method is modeled after the party Zihao getDateArr() method * @param unknown_type $start * @param unknown_type...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn