Common date operations
- /**
- * Get the start date of all weeks in the year
- * @param $year format 'YYYY'
- * Return the two-dimensional array subscript key1 value corresponding to the actual year of the week in which a certain day of the year is located, and the key2 value corresponding to the certain day of the year The day of the week [week of the year]
- * Returns false The date format is wrong
- */
- 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)));
- }
- //The number of weeks where the first Monday is located
- $begin_week_num = intval(date('W',strtotime($begin_day)));
- //One year Last Sunday
- if(date('w',strtotime($end_day))!=0){
- $end_day = date('Y-m-d',strtotime("last sunday",strtotime($end_day)));
- //There is a New Year's Eve week, and the New Year's Eve week is Monday
- $end_day_next = date('Y-m-d',strtotime($end_day)+24*60*60);
- //The year and location of the New Year's Eve week Week number
- $stride_year = date('o',strtotime($end_day_next));
- $stride_weeknum = intval(date('W',strtotime($end_day_next)));
- }
- //The number of weeks in which the last Sunday is located
- $end_week_num = intval(date('W',strtotime($end_day)));
- //The first Monday of the year is the first or second week of the 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 = '2013';
- if(get_all_weeks($year)){
- var_dump(get_all_weeks($year));
- }else{
- echo 'Date format error';
- }
- */
- /**
- * Get the start date of the week where a certain day is located
- * Depends on the function get_all_weeks
- * @param $day format: 'YYYY-mm-dd'
- * Returns false because the date format is wrong
- * Correct, 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);
- //The year and week number of a certain 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];
- //New 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 = '2014-01-01';
- if(get_day_week ($day)){
- var_dump(get_day_week($day));
- }else{
- echo 'Date format error';
- }
- */
Copy code
|