Implement the function of pushing down n months from the specified time

Source: Internet
Author: User
Tags ereg
Author: bjbs_270 a period ago, I used the question-based function for one-month report statistics. I wrote one using the mktime () function, but it was always wrong and depressing. I would like to take some time over the weekend to write one for some villagers. PHP code: --------------------------------- SyntaxHig

Author: bjbs_270
Some time ago, I made a monthly report statistics and used the topic-like function. I wrote one using the mktime () function, but it was always wrong and depressing. I would like to take some time over the weekend to write one for some villagers.
PHP code :--------------------------------------------------------------------------------
/*
* Author: heart lamp
* Function: implements the function of pushing down N months from the specified time.
* Month ($ ymd, $ len) $ ymd Time, $ len released several months
* $ Ymd = 2005-01-01;
*/
Function month ($ ymd =, $ len = 12 ){
$ Month = array ();
$ Lang = $ lang ." ";
If ($ ymd) {// Determine 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 <$ len; $ I ++ ){


If ($ I = 0 ){
$ Mktime = $ ymd? Strtotime ($ ymd): time ();
}
$ Month [] = date ("Y-m", $ mktime); // you can design your desired format as needed.
$ 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

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.