To implement a function that pushes n months down from a specified time

Source: Internet
Author: User
Tags date exit ereg implement php code
Timing | function Author: bjbs_270
Some time ago to do one months report statistics with the title of the function, combined with others with mktime () function I wrote a but then always error, depressed pole. Spend a little time on the weekend to write a use, I hope it can be useful to some villagers.

PHP Code:--------------------------------------------------------------------------------

?
/*
* Author: Heart lamp
* Function: To implement a function that pushes n months down from a specified time
* Month ($YMD, $len) $ymd time, $len launch for several months
* $ymd = ' 2005-01-01 ';
*/
Function month ($ymd = ', $len = ' 12 ') {
$month = Array ();
$lang = $lang. " <meta http-equiv=\ "content-type\" content=\ "text/html; Charset=gb2312\ ">";
if ($YMD) {//To determine if the time format is correct
if (!ereg ([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}), $ymd)) {
echo "<script>alert (the date format is not in the function argument); History.back () </script>";
Exit
}
}
if ($len) {
if (!ereg ("[0-9]+", $len)) {
echo $lang. " <script>alert (' The length of time in the function contains illegal fields '); History.back () </script>;
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 ("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. " <br> ";
}
?>
--------------------------------------------------------------------------------

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



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.