网站中常需要统计某些数据,通常都需要使用到日期来筛选统计,因为很多时候都需要使用到日期计算,用于数据库查询使用。这几天做一些统计功能时,需要使用到日期相关操作,最后写成了通用的函数,这里就分享出来给大家!
获取上个月的日期范围
如果想要获取上个月的第一天和最后一天的日期,通过这个函数可以得到:functionget_last_month_days($date){
$timestamp=strtotime($date);
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday+1month-1day"));
returnarray($firstday,$lastday);
}
获取下个月的日期范围//获取下个月的日期
functionget_next_month_days($date){
$timestamp=strtotime($date);
$arr=getdate($timestamp);
if($arr['mon']==12){
$year=$arr['year']+1;
$month=$arr['mon']-11;
$firstday=$year.'-0'.$month.'-01';
$lastday=date('Y-m-d',strtotime("$firstday+1month-1day"));
}else{
$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday+1month-1day"));
}
returnarray($firstday,$lastday);
}
获取某日期所有月份第一天和最后一天
如果做季度表时,需要计算某一个月的绩效等,就需要这样的函数将该月的第一天和最后一天的时间返回://获取指定日期所在的月份的第一天的开始时间和最后一天的结束时间
functionget_month_begin_end($date){
$timestamp=strtotime($date);
$start_time=date('Y-m-100:00:00',$timestamp);
$mdays=date('t',$timestamp);
$end_time=date('Y-m-'.$mdays.'23:59:59',$timestamp);
return[$start_time,$end_time];
}
关键词: