Home  >  Article  >  Backend Development  >  PHP code to get the start date and end date of the week (month) where the current date is located

PHP code to get the start date and end date of the week (month) where the current date is located

WBOY
WBOYOriginal
2016-07-25 08:58:31944browse
  1. // Get the start time and end time of the week on the specified date

  2. //Organize the programmer's home
  3. //at 2013-6-18
  4. function getWeekRange($ date){
  5. $ret=array();
  6. $timestamp=strtotime($date);
  7. $w=strftime('%u',$timestamp);
  8. $ret['sdate']=date('Y-m-d 00 :00:00',$timestamp-($w-1)*86400);
  9. $ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400) ;
  10. return $ret;
  11. }

  12. // Get the start date and end date of the month of the specified date

  13. function getMonthRange($date){
  14. $ret=array();
  15. $timestamp =strtotime($date);
  16. $mdays=date('t',$timestamp);
  17. $ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
  18. $ret ['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
  19. return $ret;
  20. }

  21. // The above two Application of function

  22. function getFilter($n){
  23. $ret=array();
  24. switch($n){
  25. case 1:// Yesterday
  26. $ret['sdate']=date('Y-m-d 00:00: 00',strtotime('-1 day'));
  27. $ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
  28. break;
  29. case 2: //This week
  30. $ret=getWeekRange(date('Y-m-d'));
  31. break;
  32. case 3://Previous week
  33. $strDate=date('Y-m-d',strtotime('-1 week '));
  34. $ret=getWeekRange($strDate);
  35. break;
  36. case 4: //Last week
  37. $strDate=date('Y-m-d',strtotime('-2 week'));
  38. $ ret=getWeekRange($strDate);
  39. break;
  40. case 5: //This month
  41. $ret=getMonthRange(date('Y-m-d'));
  42. break;
  43. case 6://Last month
  44. $strDate= date('Y-m-d',strtotime('-1 month'));
  45. $ret=getMonthRange($strDate);
  46. break;
  47. }
  48. return $ret;
  49. }
  50. ?>

Copy code


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