分享一段PHP时间段划分的代码。以今日为基准,计算出今天,昨天,本周、上周、本月、上月等时间段而且各个时间段不重复。
function getOutTimeRanges() {
$now = time(); // 当前时间戳
$today = strtotime('today'); // 今天零点时间戳
$yesterday = strtotime('yesterday'); // 昨天零点时间戳
$yesterday_start = $yesterday;
$yesterday_end = $today - 1;
$this_week_start = strtotime('this week 00:00:00'); // 本周第一天零点时间戳
// return date('Y-m-d H:i:s',$this_week_start);
$this_week_end = strtotime('this week +6 days 23:59:59'); // 本周最后一天最后一秒时间戳
$last_week_start = strtotime('last week 00:00:00'); // 上周第一天零点时间戳
$last_week_end = strtotime('last week +6 days 23:59:59'); // 上周最后一天最后一秒时间戳
$this_month_start = strtotime('first day of this month 00:00:00'); // 本月第一天零点时间戳
$this_month_end = strtotime('last day of this month 23:59:59'); // 本月最后一天最后一秒时间戳
$last_month_start = strtotime('first day of last month 00:00:00'); // 上月第一天零点时间戳
$last_month_end = strtotime('last day of last month 23:59:59'); // 上月最后一天最后一秒时间戳
// 去除交叉部分
$today_start = strtotime('today 00:00:00');
$today_end = strtotime('today 23:59:59');
//今天和本周冲突
if ($today_start >= $this_week_start && $today_end <= $this_week_end ) {
if ($today_start == $this_week_start){
$this_week_start =0; // 本周第一天零点时间戳
$this_week_end = 0; // 本周最后一天最后一秒时间戳
}else{
$this_week_end = strtotime('-1 day', $today_end);
}
}
//昨天和本周冲突
if ($this_week_start > 0 && $yesterday_start >= $this_week_start && $yesterday_end <= $this_week_end ) {
if ($yesterday_start == $this_week_start){
$this_week_start = 0; // 本周第一天零点时间戳
$this_week_end = 0; // 本周最后一天最后一秒时间戳
}else{
$this_week_end = strtotime('-1 day', $yesterday_end);
}
}
//return date('Y-m-d H:i:s',$this_week_end);
//如果今天和本周是一天
//交叉月份
if ($this_week_start == 0){
if ($today_start >= $this_month_start && $today_end <= $this_month_end) {
if ($today_start == $this_month_start){
$this_month_start = 0; // 本周第一天零点时间戳
$this_month_end = 0; // 本周最后一天最后一秒时间戳
}
$this_month_end = strtotime('-1 day', $today_end);
}
if ($yesterday_start >= $this_month_start && $yesterday_end <= $this_month_end) {
if ($yesterday_start == $this_month_start){
$this_month_start = 0; // 本周第一天零点时间戳
$this_month_end = 0; // 本周最后一天最后一秒时间戳
}
$this_month_end= strtotime('-1 day', $yesterday_end);
}
}else{
if ($this_week_start >= $this_month_start && $this_week_start <= $this_month_end) {
if ($today_start == $this_month_start){
$this_month_start = 0; // 本周第一天零点时间戳
$this_month_end = 0; // 本周最后一天最后一秒时间戳
}
$this_month_end = strtotime('-1 day', $this_week_start);
}
}
//上周和昨天
if ($yesterday_end == $last_week_end ) {
$last_week_end = strtotime('-1 day', $yesterday_end);
}
//上周和本月
if ($last_week_start > $this_month_start){
$this_month_end = strtotime('-1 second', $last_week_start);
}
//上周和上月
if ($last_week_start < $last_month_end){
$last_month_end = strtotime('-1 second', $last_week_start);
}
// 格式化时间段
$yesterday_start = date('Y-m-d H:i:s',$yesterday_start);
$yesterday_end = date('Y-m-d H:i:s',$yesterday_end);
$today_start = date('Y-m-d H:i:s',$today_start);
$today_end = date('Y-m-d H:i:s',$today_end);
$this_week_start = date('Y-m-d H:i:s',$this_week_start);
$this_week_end = date('Y-m-d H:i:s',$this_week_end);
$last_week_start = date('Y-m-d H:i:s',$last_week_start);
$last_week_end = date('Y-m-d H:i:s',$last_week_end);
$this_month_start = date('Y-m-d H:i:s',$this_month_start);
$this_month_end = date('Y-m-d H:i:s',$this_month_end);
$other_start = '2023-01-01 00:00:00';
$other_end = date('Y-m-d H:i:s',strtotime('-1 second', $last_month_start));
$last_month_start = date('Y-m-d H:i:s',$last_month_start);
$last_month_end = date('Y-m-d H:i:s',$last_month_end);
// 返回时间段数组
return array(
'today' => array('start' => $today_start, 'end' => $today_end),
'yesterday' => array('start' => $yesterday_start, 'end' => $yesterday_end),
'this_week' => array('start' => $this_week_start, 'end' => $this_week_end),
'last_week' => array('start' => $last_week_start, 'end' => $last_week_end),
'this_month' => array('start' => $this_month_start, 'end' => $this_month_end),
'last_month' => array('start' => $last_month_start, 'end' => $last_month_end),
'other'=>array('start'=>$other_start,'end'=>$other_end),
);
}