以星期一作为 一周的开始, 以 星期日 作为 一周的结束.
1 date_default_timezone_set('PRC'); 2 3 4 function getWeek() 5 { 6 //本周: 7 $thisWeek['start'] = mktime(0, 0, 0, date('m'), date('d')-date('w')+1, date('Y')); 8 $thisWeek['end'] = mktime(23, 59, 59, date('m'), date('d')+7-date('w'), date('Y')); 9 $thisWeek['startStr'] = date('Y-m-d H:i:s', $thisWeek['start']); 10 $thisWeek['endStr'] = date('Y-m-d H:i:s', $thisWeek['end']); 11 12 //上一周: 13 $beforeWeek['start'] = mktime(0, 0, 0, date('m'), date('d')-date('w')+1-7, date('Y')); 14 $beforeWeek['end'] = mktime(23, 59, 59, date('m'), date('d')+7-date('w')-7, date('Y')); 15 $beforeWeek['startStr'] = date('Y-m-d H:i:s', $beforeWeek['start']); 16 $beforeWeek['endStr'] = date('Y-m-d H:i:s', $beforeWeek['end']); 17 18 //下一周: 19 $nextWeek['start'] = mktime(0, 0, 0, date('m'), date('d')-date('w')+1+7, date('Y')); 20 $nextWeek['end'] = mktime(23, 59, 59, date('m'), date('d')+7-date('w')+7, date('Y')); 21 $nextWeek['startStr'] = date('Y-m-d H:i:s', $nextWeek['start']); 22 $nextWeek['endStr'] = date('Y-m-d H:i:s', $nextWeek['end']); 23 24 $week['beforeWeek'] = $beforeWeek; 25 $week['thisWeek'] = $thisWeek; 26 $week['nextWeek'] = $nextWeek; 27 return $week; 28 29 30 } 31 32 var_dump(getWeek());
当前时间: 2017-05-03:
1 array(3) { 2 ["beforeWeek"]=> 3 array(4) { 4 ["start"]=> 5 int(1492963200) 6 ["end"]=> 7 int(1493567999) 8 ["startStr"]=> 9 string(19) "2017-04-24 00:00:00" 10 ["endStr"]=> 11 string(19) "2017-04-30 23:59:59" 12 } 13 ["thisWeek"]=> 14 array(4) { 15 ["start"]=> 16 int(1493568000) 17 ["end"]=> 18 int(1494172799) 19 ["startStr"]=> 20 string(19) "2017-05-01 00:00:00" 21 ["endStr"]=> 22 string(19) "2017-05-07 23:59:59" 23 } 24 ["nextWeek"]=> 25 array(4) { 26 ["start"]=> 27 int(1494172800) 28 ["end"]=> 29 int(1494777599) 30 ["startStr"]=> 31 string(19) "2017-05-08 00:00:00" 32 ["endStr"]=> 33 string(19) "2017-05-14 23:59:59" 34 } 35 }
查看: