To implement a function that pushes n months from a specified time _php tutorial

Source: Internet
Author: User
Tags ereg
Author: bjbs_270
A period of time to do one months report statistics with the title function, combined with others with mktime () function I wrote a But then always wrong, depressed pole. Take a little time on weekends to write one, and hopefully it will be useful to some villagers.
PHP Code:--------------------------------------------------------------------------------
/*
* Author: Heart lamp
* Function: Implements a function that pushes n months from a specified time
* Month ($YMD, $len) $ymd time, $len launch months
* $ymd =2005-01-01;
*/
Function month ($ymd =, $len =12) {
$month = Array ();
$lang = $lang. " ";
if ($YMD) {//Determine if 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 < $len; $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;
$t 1 = $mktime;
$mktime =date ("y-m-d", Mktime (Date ("H", $t 1), date ("I", $t 1), date ("s", $t 1), date ("M", $t 1), date ("D", $t 1)-$reday, date ( "Y", $t 1)));
$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

http://www.bkjia.com/PHPjc/532010.html www.bkjia.com true http://www.bkjia.com/PHPjc/532010.html techarticle Author: bjbs_270 A period of time to do one months report statistics with the title function, combined with others with mktime () function I wrote a But then always wrong, depressed pole. Take a weekend off ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.