Code for PHP to take the start date and end date of the week (month) of the current date

Source: Internet
Author: User
  1. Gets the start time and end time of the week of the specified date

  2. Tidy up the programmer's house
  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. Gets the start date and end date of the month on which the specified date is located

  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. 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 [' 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://on one weeks
  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
  • Related Article

    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.