Php code for retrieving the start date and end date of the week (month) of the current date

Source: Internet
Author: User
Php code for retrieving the start date and end date of the week (month) of the current date

  1. // Obtain the start time and end time of the week of 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 ['update'] = date ('Y-m-d 23:59:59 ', $ timestamp + (7-$ w) * 86400 );
  10. Return $ ret;
  11. }

  12. // Obtain 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 ['update'] = date ('Y-m-'. $ mdays. '23:59:59', $ timestamp );
  19. Return $ ret;
  20. }

  21. // Application of the above two functions

  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 ['update'] = 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: // last 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. ?>

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.