Obtain the start date of the week based on the given date format & apos; YYYY-mm-dd & apos;

Source: Internet
Author: User
Tags preg
Obtain the start date of the week based on the given date format & amp; apos; YYYY-mm-dd & amp; apos;
Common Date Operations

  1. /**
  2. * Get the start date of all weeks in a year
  3. * @ Param $ year format 'yyyy'
  4. * Returns the two-dimensional array subscript key1 value corresponding to the actual year key2 value of the week of a day in a year corresponding to the number of weeks of a day in a year [week of a 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. // The 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. // Week number of the first Monday
  21. $ Begin_week_num = intval (date ('W', strtotime ($ begin_day )));
  22. // 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 week of the new year, and the week and Monday of the new year
  26. $ End_day_next = date ('Y-m-D', strtotime ($ end_day) + 24*60*60 );
  27. // Year and number of weeks of the cross-year week
  28. $ Stride_year = date ('O', strtotime ($ end_day_next ));
  29. $ Stride_weeknum = intval (date ('W', strtotime ($ end_day_next )));
  30. }
  31. // Week number of the last Sunday
  32. $ End_week_num = intval (date ('W', strtotime ($ end_day )));
  33. // The first Monday of a year is the first or second week of a 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 = '000000 ';
  60. If (get_all_weeks ($ year )){
  61. Var_dump (get_all_weeks ($ year ));
  62. } Else {
  63. Echo 'date format error ';
  64. }
  65. */
  66. /**
  67. * Obtain the start date of the week in which a day is located.
  68. * Dependent function get_all_weeks
  69. * @ Param $ day format: 'yyyy-mm-DD'
  70. * Return false date format error
  71. * Correct. json "{" begin_day ":" YYYY-mm-dd "," end_day ":" YYYY-mm-dd "}" is returned "}"
  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. // Year and week of a day
  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. // The cross-year week value is 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 = '2017-01-01 ';
  108. If (get_day_week ($ day )){
  109. Var_dump (get_day_week ($ day ));
  110. } Else {
  111. Echo 'date format error ';
  112. }
  113. */

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.