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

Source: Internet
Author: User
Tags ereg php source code

Introduction: This is a detailed page for pushing down functions for N months from a specified time. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 326942 'rolling = 'no'>

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.

PHPCode:--------------------------------------------------------------------------------

<?

/*

* 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 = '2017-01-01 ';

*/

Function month ($ ymd = ', $ Len = '12 '){

$ Month = array ();

$ Lang = $ Lang. "<meta http-equiv = \" Content-Type \ "content = \" text/html; charset = gb2312 \ "> ";

If ($ ymd) {// determine whether the time format is correct

If (! Ereg ("([0-9] {4})-([0-9] {1, 2})-([0-9] {1, 2 })", $ ymd )){

Echo "<SCRIPT> alert ('date format in function parameters'); history. Back () </SCRIPT> ";

Exit;

}

}

If ($ Len ){

If (! Ereg ("[0-9] +", $ Len )){

Echo $ Lang. "<SCRIPT> alert ('invalid time length in the function '); 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 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. "<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

More articles about how to push down functions for N months at a specified time

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/326942.html pageno: 13.

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.