//生成一段时间 返回这段时间每一天组成的数组
function getTimeInterval($start_time, $end_time)
{
$time = array();
$tmp_time = date('Y-m-d', strtotime($start_time));
$time[] = $tmp_time;
while ($tmp_time < $end_time){
$tmp_time = date("Y-m-d",strtotime("$tmp_time +1 day"));
$time[] = $tmp_time;
}
return $time;
}
//生成一段时间 返回这段时间每一月组成的数组
function getMonthInterval($start_time, $end_time)
{
$time = array();
$tmp_time = date('Y-m', strtotime($start_time));
$time[] = $tmp_time;
while ($tmp_time < $end_time){
$tmp_time = date("Y-m",strtotime("$tmp_time +1 month"));
$time[] = $tmp_time;
}
return $time;
}
//生成一段时间, 返回这段时间每年 组成的数组
function getYearInterval($start_time, $end_time)
{
$tmp_year = date('Y', strtotime($start_time));
$end_year = date('Y', strtotime($end_time));
$time[] = $tmp_year;
while($tmp_year < $end_year){
$tmp_year += 1;
$time[] = $tmp_year;
}
return $time;
}
//计算某一个月份的 最后一天
function getLastDay($date)
{
$firstDay = getFirstDay($date);
return date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
}
//获取当前月份 前一个月份的第一天
function getLatelyMonthFirstDay()
{
$timestamp=time();
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
return $firstday;
}
//获取当前月份 前一个月份的最后一天
function getLatelyMonthLastDay()
{
$firstday=getLatelyMonthFirstDay();
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
return $lastday;
}
//返回一周之前的日期y-m-d
function getLastWeek()
{
return date('Y-m-d',time()-7*24*3600);
}
//获取时间间隔的天数$start, $end 2008-9-11 2009-10-13
function getIntervalDays($start, $end)
{
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($end);
$interval = $datetime1->diff($datetime2);
return $interval->days;
}
//获取月份的季度
function getSeasonByDate($date)
{
$m = date('m',strtotime($date));
$season = ceil($m/3);
return $season;
}
//获取本周开始结束时间 end_day 结束的日期(星期1,2,3...)
function getThisWeekStartAndEndTime($current_date,$end_day = 7)
{
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$first=1;
$range = $end_day - $first;
//获取当前周的第几天 周日是 0 周一到周六是1 - 6
$w=date('w',strtotime($current_date));
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$week_start=date('Y-m-d',strtotime("$current_date -".($w ? $w - $first : 6).' days'));
//本周结束日期
$week_end=date('Y-m-d',strtotime("$week_start +$range days"));
return [
'start_time' => strtotime($week_start." 00:00:01"),
'end_time' => strtotime($week_end." 23:59:59"),
];
}
}