Home >Backend Development >PHP Tutorial >Implement a function that pushes down n months from a specified time_PHP tutorial

Implement a function that pushes down n months from a specified time_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:25:54994browse

Author: bjbs_270
I used the function as mentioned in the monthly report statistics some time ago. I combined it with others to use the mktime() function and wrote one, but I kept getting errors afterwards, which was very frustrating. I took some time over the weekend to write one for use. I hope it will be useful to some villagers.
PHP code:-------------------------------------------------- ------------------------------------

/*
* Author: Xin Deng
* Function: Implement a function to push down N months from a specified time
* month($ymd,$len) $ymd time, $len pushes out several months
* $ymd =2005-01-01;
*/
function month($ymd=,$len=12){
$month = array();
$lang = $lang."";
if($ymd){//Judge whether the time format is correct
if(!ereg("([0-9]{4})-([0-9]{1,2} )-([0-9]{1,2})",$ymd)){
echo "";
exit;
}
}
if($len){
if(!ereg("[0-9]+",$len)){
echo $lang."";
exit;
}
}
for( $i=0;$i


if($i==0){
$mktime = $ymd?strtotime($ymd ):time();
}
$month[] = date("Y-m",$mktime);//You can design the format you want according to your needs
$day = date("t ",$mktime);
if($i==0)
$reday = date("d",$mktime);
else
$reday = $day;
$ t1 = $mktime;
$mktime=date("Y-m-d",mktime(date("H",$t1),date("i",$t1),date("s",$t1),date ("m",$t1),date("d",$t1)-$reday,date("Y",$t1)));
$mktime =strtotime($mktime);
}
return $month;
}
$month = month();
foreach($month as $key => $v){
echo $v."
";
}
?>
----------------------------------------- ------------------------------------------
The effect is as follows:
2005-01
2004-12
2004-11
2004-10
2004-09
2004-08
2004-07
2004-06
2004 -05
2004-04
2004-03
2004-02

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532010.htmlTechArticleAuthor: bjbs_270 Some time ago I made a monthly report statistics and used the function like the title, combined with others using mktime( ) function, but it kept getting errors later, which made me very depressed. Take a day off from the weekend...
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