Obtain the start date of the week based on the given date format 'yyyy-mm-DD'-PHP source code

Source: Internet
Author: User
Tags preg
Obtain the php code for the start date of the week based on the given date format YYYY-mm-dd.

/*** 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 in a year week number of a day in a year [week of the 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); // if (date ('W ', strtotime ($ end_day ))! = 0) {$ end_day = date ('Y-m-D', strtotime ("last Sunday", strtotime ($ end_day); // There is a week across the year, monday of the New Year's week $ end_day_next = date ('Y-m-D', strtotime ($ end_day) + 24*60*60 ); // year and week of the new year $ stride_year = date ('O', strtotime ($ end_day_next); $ stride_weeknum = intval (date ('W ', strtotime ($ end_day_next);} // The number of weeks of the last Sunday $ end_week_num = intval (date ('W', strtotime ($ end_day ))); // The first Monday of a year is the first week of a year or the second week 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 $ w Eek_arr;} // demo _ call/* $ year = '000000'; if (get_all_weeks ($ year) {var_dump (get_all_weeks ($ year ));} else {echo 'date format error ';} * // *** get the start date of the week in which a day is located * depends on function get_all_weeks * @ param $ day format: 'yyyy-mm-DD' * returns false date format error *, returns json "{" begin_day ":" YYYY-mm-dd "," end_day ": "YYYY-mm-dd"} "*/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 if (empty ($ begin_day) | empty ($ end_day) {$ year = date ('Y', strtotime ($ day) of the previous year )) -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 ('In in _ 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 ';}*/

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.