/**按照 年、月、日、周、季 返回不同时间段的 起始时间与结束时间(默认按照每天返回) * @param $starttime (2017-10-01) * @param $endtime (2017-10-02) * @param string $period 时间类型 * @return mixed */ public static function getEachTime($starttime, $endtime, $period = "day") { $starttime = date('Y-m-d', strtotime($starttime)) . ' 00:00:00'; $endtime = date('Y-m-d', strtotime($endtime)) . ' 23:59:59'; $format = ""; if ($period == 'day') { $format = 'Y-m-d'; } else if ($period == 'week') { $format = 'Y-W'; } else if ($period == 'month') { $format = 'Y-m'; } else if ($period == 'year') { $format = 'Y'; } $starttime = strtotime($starttime); $endtime = strtotime($endtime); $season = ""; do { if ($period == "season") { $season = ceil((date('n', $starttime)) / 3); $key = date("Y", $starttime) . "-" . $season; } else { $key = date($format, $starttime); } if ($period == "week") { $num = date("N", $starttime); $list[$key]['starttime'] = date("Y-m-d H:i:s", strtotime('-' . ($num - 1) . 'day', $starttime)); $starttime = strtotime('+' . (8 - $num) . 'day', $starttime); } else if ($period == "season") { $num = date("n", $starttime); $insert = str_pad(($season - 1) * 3 + 1, 2, "0", STR_PAD_LEFT); $list[$key]['starttime'] = date("Y-m-d H:i:s", strtotime(date("Y", $starttime) . "-" . $insert . "-01")); $starttime = strtotime(date("Y-m", $starttime)); $starttime = strtotime('+' . ($season * 3 + 1 - $num) . 'month', $starttime); } else if ($period == "year") { $num = date("n", $starttime); $list[$key]['starttime'] = date("Y-m-d H:i:s", strtotime(date("Y", $starttime) . "-01-01")); $starttime = strtotime(date("Y-m", $starttime)); $starttime = strtotime('+' . (13 - $num) . 'month', $starttime); } else if ($period == "month") { $num = date("j", $starttime); $list[$key]['starttime'] = date("Y-m-d H:i:s", strtotime(date("Y-m", $starttime))); $totalDays = date("t", $starttime); $starttime = strtotime('+' . ($totalDays - $num + 1) . 'day', $starttime); } else { $list[$key]['starttime'] = date("Y-m-d H:i:s", $starttime); $starttime = strtotime('+1 ' . $period, $starttime); } $list[$key]['endtime'] = date("Y-m-d H:i:s", $starttime - 1); } while ($starttime < $endtime); if ($period == "season") { $season = ceil((date('n', $endtime)) / 3); $key = date("Y", $endtime) . "-" . $season; } else { $key = date($format, $endtime); } if (!isset($list[$key])) { $list[$key]['starttime'] = date("Y-m-d H:i:s", strtotime(date("Y-m-d", $endtime))); $list[$key]['endtime'] = date("Y-m-d H:i:s", $endtime); } return $list; }