Home > Backend Development > PHP Tutorial > Code to generate an array of months from the starting month to the ending month within a certain time period

Code to generate an array of months from the starting month to the ending month within a certain time period

WBOY
Release: 2016-07-25 09:05:15
Original
1339 people have browsed it
  1. /**

  2. * Generate a month array from the start month to the end month of a certain 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. The calculation formula for accumulating the total number of seconds in each month: the timestamp seconds on the 1st of the previous month minus the number of seconds in the current month Timestamp seconds
  20. */
  21. $d[$i] = trim(date('Y-m',$t),' ');
  22. $t += strtotime('+1 month', $t)- $t;
  23. $i++;
  24. }
  25. return $d;
  26. }
  27. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template