Common Date Operations
- /**
- * Get the start date of all weeks in a year
- * @ Param $ year format 'yyyy'
- * 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]
- * Return false date format error
- */
- Function get_all_weeks ($ year ){
- $ Week_arr = array ();
- $ Year = intval (trim ($ year ));
- $ Preg = "/^ \ d {4,} $ /";
- If (! Preg_match ($ preg, $ year )){
- Return false;
- }
- $ Begin_day = $ year. '-01-01 ';
- $ End_day = $ year. '-12-31 ';
- // The first Monday of the year
- If (date ('W', strtotime ($ begin_day ))! = 1 ){
- $ Begin_day = date ('Y-m-D', strtotime ("next Monday", strtotime ($ begin_day )));
- }
- // Week number of the first Monday
- $ Begin_week_num = intval (date ('W', strtotime ($ begin_day )));
- // Last Sunday of the year
- If (date ('W', strtotime ($ end_day ))! = 0 ){
- $ End_day = date ('Y-m-D', strtotime ("last Sunday", strtotime ($ end_day )));
- // There is a week of the new year, and the week and Monday of the new year
- $ End_day_next = date ('Y-m-D', strtotime ($ end_day) + 24*60*60 );
- // Year and number of weeks of the cross-year week
- $ Stride_year = date ('O', strtotime ($ end_day_next ));
- $ Stride_weeknum = intval (date ('W', strtotime ($ end_day_next )));
- }
- // Week number of the last Sunday
- $ End_week_num = intval (date ('W', strtotime ($ end_day )));
- // The first Monday of a year is the first or second week of a year.
- If ($ begin_week_num! = 1 ){
- $ I = 2;
- } Else {
- $ I = 1;
- }
- $ J = 0;
- For ($ I; $ I <= $ end_week_num; $ I ++ ){
- $ Start_date = date ("Y-m-d", strtotime ("$ begin_day $ j week "));
- $ End_day = date ("Y-m-d", strtotime ($ start_date. '+ 6 Day '));
- $ Week_arr [$ year] [$ I] = array (
- $ Start_date,
- $ End_day
- );
- $ J ++;
- }
- If ($ end_day_next ){
- $ Week_arr [$ stride_year] [$ stride_weeknum] = array (
- $ End_day_next,
- Date ("Y-m-d", strtotime ($ end_day_next. '+ 6 Day '))
- );
- }
- Return $ week_arr;
- }
- // Demo _ call
- /*
- $ Year = '000000 ';
- If (get_all_weeks ($ year )){
- Var_dump (get_all_weeks ($ year ));
- } Else {
- Echo 'date format error ';
- }
- */
- /**
- * Obtain the start date of the week in which a day is located.
- * Dependent function get_all_weeks
- * @ Param $ day format: 'yyyy-mm-DD'
- * Return false date format error
- * Correct. json "{" begin_day ":" YYYY-mm-dd "," end_day ":" YYYY-mm-dd "}" is returned "}"
- */
- Function get_day_week ($ day ){
- $ Date_arr = explode ('-', trim ($ day ));
- If (! Checkdate (intval ($ date_arr [1]), intval ($ date_arr [2]), intval ($ date_arr [0]) {
- Return false;
- }
- $ Year = date ('Y', strtotime ($ day ));
- $ Weeks = get_all_weeks ($ year );
- // Year and week of a day
- $ Real_year = date ('O', strtotime ($ day ));
- $ Week_num = intval (date ('W', strtotime ($ day )));
- If (! Empty ($ weeks [$ real_year] [$ week_num] [0])
- $ Begin_day = $ weeks [$ real_year] [$ week_num] [0];
- If (! Empty ($ weeks [$ real_year] [$ week_num] [1])
- $ End_day = $ weeks [$ real_year] [$ week_num] [1];
- // The cross-year week value is stored in the previous year
- If (empty ($ begin_day) | empty ($ end_day )){
- $ Year = date ('Y', strtotime ($ day)-1;
- $ Weeks = get_all_weeks ($ year );
- $ Real_year = date ('O', strtotime ($ day ));
- $ Week_num = intval (date ('W', strtotime ($ day )));
- If (! Empty ($ weeks [$ real_year] [$ week_num] [0])
- $ Begin_day = $ weeks [$ real_year] [$ week_num] [0];
- If (! Empty ($ weeks [$ real_year] [$ week_num] [1])
- $ End_day = $ weeks [$ real_year] [$ week_num] [1];
- }
- $ The_day = array (
- 'Begin_day' => $ begin_day,
- 'End _ day' => $ end_day
- );
- $ The_day = json_encode ($ the_day );
- Return $ the_day;
- }
- // Demo _ call
- /*
- $ Day = '2017-01-01 ';
- If (get_day_week ($ day )){
- Var_dump (get_day_week ($ day ));
- } Else {
- Echo 'date format error ';
- }
- */
|