Home  >  Article  >  Backend Development  >  PHP time and date processing and sorting

PHP time and date processing and sorting

WBOY
WBOYOriginal
2016-07-25 08:43:48754browse
  1. //Return the timestamps of all months in a time period
  2. function monthList($start,$end){
  3. if(!is_numeric($start)||!is_numeric($end)||($end< ;=$start)) return '';
  4. $start=date('Y-m',$start);
  5. $end=date('Y-m',$end);
  6. //Convert to timestamp
  7. $start=strtotime($start.'-01');
  8. $end=strtotime($end.'-01');
  9. $i=0;
  10. $d=array();
  11. while($start<= $end){
  12. //The calculation formula for the total number of seconds accumulated in each month is: the timestamp seconds on the 1st of the previous month minus the timestamp seconds of the current month
  13. $d[$i]=trim(date ('Y-m',$start),' ');
  14. $start+=strtotime('+1 month',$start)-$start;
  15. $i++;
  16. }
  17. return $d;
  18. }
  19. //Return the start and end dates of the week within a time period by passing date type
  20. function monthList($start,$end){
  21. if(!is_numeric($start)||!is_numeric($end)||($end< ;=$start)) return '';
  22. $start=date('Y-m',$start);
  23. $end=date('Y-m',$end);
  24. //Convert to timestamp
  25. $start=strtotime($start.'-01');
  26. $end=strtotime($end.'-01');
  27. $i=0;
  28. $d=array();
  29. while($start<= $end){
  30. //The calculation formula for the total number of seconds accumulated in each month is: the timestamp seconds on the 1st of the previous month minus the timestamp seconds of the current month
  31. $d[$i]=trim(date ('Y-m',$start),' ');
  32. $start+=strtotime('+1 month',$start)-$start;
  33. $i++;
  34. }
  35. return $d;
  36. }
  37. / /Return the first and last day of a month
  38. function getthemonth($date)
  39. {
  40. $firstday = date('Y-m-01', strtotime($date));
  41. $lastday = date('Y-m-d ', strtotime("$firstday +1 month -1 day"));
  42. return array($firstday,$lastday);
  43. }
  44. $today = date("Y-m-d");
  45. $day=getthemonth($today) ;
Copy code

PHP


Statement:
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
Previous article:PHP reads csv file classNext article:PHP reads csv file class