Based on the given date format ' Yyyy-mm-dd' Gets the start date of the week on which the date is located

Source: Internet
Author: User
Tags preg
Common date Operations
  1. /**
  2. * Get the start date of all weeks of the year
  3. * @param $year format ' YYYY '
  4. * Returns the two-dimensional array subscript key1 value corresponds to the actual year of the week of the year Key2 value corresponds to the number of weeks of the year in which the day is located [the week ordinal of the year]
  5. * Return False Date format error
  6. */
  7. function Get_all_weeks ($year) {
  8. $week _arr = Array ();
  9. $year = Intval (Trim ($year));
  10. $preg = "/^\d{4,}$/";
  11. if (!preg_match ($preg, $year)) {
  12. return false;
  13. }
  14. $begin _day = $year. '-01-01 ';
  15. $end _day = $year. '-12-31 ';
  16. First Monday of the year
  17. if (Date (' W ', Strtotime ($begin _day))!=1) {
  18. $begin _day = Date (' y-m-d ', Strtotime ("Next Monday", Strtotime ($begin _day)));
  19. }
  20. Number of weeks in first Monday
  21. $begin _week_num = intval (Date (' W ', Strtotime ($begin _day));
  22. One last Sunday of the year
  23. if (Date (' W ', Strtotime ($end _day))!=0) {
  24. $end _day = Date (' y-m-d ', Strtotime ("Last Sunday", Strtotime ($end _day)));
  25. There is a cross-year week, a cross-year week on Monday
  26. $end _day_next = Date (' y-m-d ', Strtotime ($end _day) +24*60*60);
  27. Year of year and number of weeks in which the week is spanned
  28. $stride _year = date (' O ', Strtotime ($end _day_next));
  29. $stride _weeknum = intval (Date (' W ', Strtotime ($end _day_next));
  30. }
  31. Number of weeks last Sunday
  32. $end _week_num = intval (Date (' W ', Strtotime ($end _day));
  33. The first Monday of the year is the first week or the second week of the year.
  34. if ($begin _week_num!=1) {
  35. $i = 2;
  36. }else{
  37. $i = 1;
  38. }
  39. $j = 0;
  40. for ($i; $i <= $end _week_num; $i + +) {
  41. $start _date = Date ("y-m-d", Strtotime ("$begin _day $j Week"));
  42. $end _day = Date ("y-m-d", Strtotime ($start _date. ' +6 Day ');
  43. $week _arr[$year [$i] = Array (
  44. $start _date,
  45. $end _day
  46. );
  47. $j + +;
  48. }
  49. if ($end _day_next) {
  50. $week _arr[$stride _year][$stride _weeknum] = Array (
  51. $end _day_next,
  52. Date ("Y-m-d", Strtotime ($end _day_next. ' +6 Day '))
  53. );
  54. }
  55. return $week _arr;
  56. }
  57. Demo_ Call
  58. /*
  59. $year = ' 2013 ';
  60. if (Get_all_weeks ($year)) {
  61. Var_dump (Get_all_weeks ($year));
  62. }else{
  63. Echo ' date format error ';
  64. }
  65. */
  66. /**
  67. * Gets the start date of the week in which the day is located
  68. * Dependent function Get_all_weeks
  69. * @param $day format: ' Yyyy-mm-dd '
  70. * Return False Date format error
  71. * Correct, return JSON "{" Begin_day ":" Yyyy-mm-dd "," End_day ":" Yyyy-mm-dd "}"
  72. */
  73. function Get_day_week ($day) {
  74. $date _arr = Explode ('-', trim ($day));
  75. if (!checkdate (Intval ($date _arr[1)), Intval ($date _arr[2]), Intval ($date _arr[0])) {
  76. return false;
  77. }
  78. $year = Date (' Y ', Strtotime ($day));
  79. $weeks = Get_all_weeks ($year);
  80. The number of years and weeks that a certain day belongs to
  81. $real _year = date (' O ', Strtotime ($day));
  82. $week _num = intval (Date (' W ', Strtotime ($day)));
  83. if (!empty ($weeks [$real _year][$week _num][0])
  84. $begin _day = $weeks [$real _year][$week _num][0];
  85. if (!empty ($weeks [$real _year][$week _num][1])
  86. $end _day = $weeks [$real _year][$week _num][1];
  87. Year-over-week values are stored in the previous year
  88. if (Empty ($begin _day) | | Empty ($end _day)) {
  89. $year = Date (' Y ', Strtotime ($day))-1;
  90. $weeks = Get_all_weeks ($year);
  91. $real _year = date (' O ', Strtotime ($day));
  92. $week _num = intval (Date (' W ', Strtotime ($day)));
  93. if (!empty ($weeks [$real _year][$week _num][0])
  94. $begin _day = $weeks [$real _year][$week _num][0];
  95. if (!empty ($weeks [$real _year][$week _num][1])
  96. $end _day = $weeks [$real _year][$week _num][1];
  97. }
  98. $the _day = Array (
  99. ' Begin_day ' = $begin _day,
  100. ' End_day ' = $end _day
  101. );
  102. $the _day = Json_encode ($the _day);
  103. return $the _day;
  104. }
  105. Demo_ Call
  106. /*
  107. $day = ' 2014-01-01 ';
  108. if (Get_day_week ($day)) {
  109. Var_dump (Get_day_week ($day));
  110. }else{
  111. Echo ' date format error ';
  112. }
  113. */
Copy Code
  • 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.