/** * 两个日期这间的间隔天数 * @param $date1 * @param $date2 * @return float */ public static function daysBetweenDates($date1, $date2) { $days = floor(abs($date1 - $date2) / 86400); return $days; } /** * 获取两个时间间隔之间的所有日期 * @param string|int $minDay like "2015-05-05" or unix_timestamp * @param string|int $maxDay like "2015-06-05" or unix_timestamp * @return array ['2015-050-05','2015-05-06',...] */ public static function getAllDays($minDay, $maxDay) { $minTime = is_numeric($minDay) ? intval($minDay) : strtotime($minDay); $maxTime = is_numeric($maxDay) ? intval($maxDay) : strtotime($maxDay); $res = []; for ($current = $minTime; $current <= $maxTime; $current = strtotime(date("Y-m-d", $current) . " +1 day")) { $res[] = date("Y-m-d", $current); } return $res; } /** * 获取两个时间间隔之间的所有星期 (当年的第几周) * @param string|int $minDay like "2015-05-05" or unix_timestamp * @param string|int $maxDay like "2015-06-05" or unix_timestamp * @return array ['2015 05','2015 06',...] */ public static function getAllWeeks($minDay, $maxDay) { $minTime = is_numeric($minDay) ? intval($minDay) : strtotime($minDay); $maxTime = is_numeric($maxDay) ? intval($maxDay) : strtotime($maxDay); $year = date("Y", $minTime); $week = intval(date("N", $minTime)); $startTime = $minTime - ($week - 1) * 86400; $startYear = date("Y", $startTime); if ($startYear != $year) { $startTime = strtotime("{$year}-01-01"); } $endWeek = intval(date("N", $maxTime)); $endTime = $maxTime + (7 - $endWeek) * 86400; $res = []; for ($current = $startTime; $current <= $endTime; $current = strtotime(date("Y-m-d", $current) . " +1 week")) { $res[] = date("Y W", $current); } return $res; } /** * 获取两个时间间隔之间的所有星期 (第几周) * @param string|int $minDay like "2015-05-05" or unix_timestamp * @param string|int $maxDay like "2015-06-05" or unix_timestamp * @return array ['2015-05','2015-06',...] */ public static function getAllMonth($minDay, $maxDay) { $minTime = is_numeric($minDay) ? intval($minDay) : strtotime($minDay); $maxTime = is_numeric($maxDay) ? intval($maxDay) : strtotime($maxDay); $startTime = strtotime(date("Y-m-01", $minTime)); $res = []; for ($current = $startTime; $current <= $maxTime; $current = strtotime(date("Y-m-d", $current) . " +1 month")) { $res[] = date("Y-m", $current); } return $res; }