Code to generate a month array from the start month to the end month in a time period

Source: Internet
Author: User
Code to generate a month array from the start month to the end month in a time period

  1. /**

  2. * Generates an array of months from the start month to the end month of a time period.
  3. * @ Param unknown_type $ start
  4. * @ Param unknown_type $ end
  5. * Url: http://bbs.it-home.org
  6. */
  7. Function getMonthArr ($ start, $ end)
  8. {
  9. $ Start = empty ($ start )? Date ('Y-M', strtotime ('-1 month'): $ start;
  10. $ End = empty ($ end )? Date ('Y-M'): $ end;

  11. // Convert to timestamp

  12. $ St = strtotime ($ start. '-01 ');
  13. $ Et = strtotime ($ end. '-01 ');

  14. $ T = $ st;

  15. $ I = 0;
  16. While ($ t <= $ et)
  17. {
  18. /*
  19. Formula for calculating the total number of seconds of each month: the number of seconds in the previous month minus the number of seconds in the current month
  20. */
  21. $ D [$ I] = trim (date ('Y-M', $ t ),'');
  22. $ T + = strtotime ('+ 1 month', $ t)-$ t;
  23. $ I ++;
  24. }
  25. Return $ d;
  26. }
  27. ?>

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.