/*
根据起止时间,输出按天时间的数组
section_day('2020-1-1 12:20','2020-2-12');
*/
function section_day($sdate,$edate){
$arr = array();
//$week = array('日','一','二','三','四','五','六');
for($i=0;strtotime($sdate.'+'.$i.' days')<=strtotime($edate)&&$i<365;$i++){
$time = strtotime($sdate.'+'.$i.' days');
$arr[$i]['date'] = date('Y-m-d',$time);
}
return $arr;
}
/*
根据起止时间,输出按小时的数组
section_hour('2020-1-1 12:20','2020-1-1 21:30');
*/
function section_hour($stime,$etime){
$arr = array();
//$week = array('日','一','二','三','四','五','六');
for($i=0;strtotime($stime.'+'.$i.' hour')<strtotime($etime)&&$i<24;$i++){
$time = strtotime($stime.'+'.$i.' hour');
$arr[$i]['date'] = date('Y-m-d',$time); //,'(',$week[date('w',$time)],")";
$arr[$i]['hour'] = date('H',$time);
}
return $arr;
}
/**
* 把秒数转换为时分秒的格式
* @param Int $times 时间,单位 秒
* @return String
*/
function secToTime($times){
$result = '00:00:00';
if ($times>0) {
//$hour = floor($times/3600);
$minute = floor($times/60);
if(strlen($minute)==1){
$minute = "0".$minute;
}
$second = floor(($times - 60 * $minute) % 60);
if(strlen($second)==1){
$second = "0".$second;
}
$result = $minute.':'.$second;
}
return $result;
}
/**
* 将日期格式根据以下规律修改为不同显示样式
* @static
* @param $sorce_date 时间戳
* @return void
*/
function get_date_style($sorce_date){
$time = time(); //获取今天时间戳
$time_less = $time - $sorce_date;
switch($time_less){
case $time_less < 60:
$timeHtml = $time_less ."秒前";
break;
case $time_less >=60 && $time_less < 3600:
$temp_time = floor($time_less/60);
$timeHtml = $temp_time ."分钟前";
break;
case $time_less >=3600 && $time_less < 3600*24:
$temp_time = floor($time_less/3600);
$timeHtml = $temp_time .'小时前';
break;
case $time_less >= 3600*24 && $time_less < 3600*24*7:
$temp_time = floor($time_less/(3600*24));
$timeHtml = $temp_time.'天前';
break;
case $time_less >=3600*24*7 && $time_less < 3600*24*7*4:
$temp_time = floor($time_less/(3600*24*7));
$timeHtml = $temp_time.'周前';
break;
case $time_less >=3600*24*7*4 && $time_less < 3600*24*7*4*12:
$temp_time = floor($time_less/(3600*24*7*4));
$timeHtml = $temp_time.'月前';
break;
default:
$timeHtml = date('Y-m-d',$sorce_date);
break;
}
return empty($timeHtml) ? '' : $timeHtml;
}
/*
* 返回输入日期星期几
* @param $date 日期
* */
function get_week($date){
$date_str=date('Y-m-d',strtotime($date));
$arr=explode("-", $date_str);
$year=$arr[0];
$month=sprintf('%02d',$arr[1]);
$day=sprintf('%02d',$arr[2]);
$hour = $minute = $second = 0;
$strap = mktime($hour,$minute,$second,$month,$day,$year);
$number_wk=date("w",$strap);
$weekArr=array("周日","周一","周二","周三","周四","周五","周六");
return $weekArr[$number_wk];
}
/**
* 求两个日期之间相差的天数
* (针对1970年1月1日之后,求之前可以采用泰勒公式)
* @param string $start_day
* @param string $end_day
* @return number
*/
function diffBetweenTwoDays($start_day, $end_day){
$second1 = strtotime($start_day);
$second2 = strtotime($end_day);
if ($second1 < $second2) {
$tmp = $second2;
$second2 = $second1;
$second1 = $tmp;
}
return ($second1 - $second2) / 86400;
}
/*昨天*/
function yesterday() {
$start_terday = strtotime(date("Y-m-d 00:00:00", strtotime("-1 day")));
$end_terday = strtotime(date("Y-m-d 23:59:59", strtotime("-1 day")));
return array($start_terday,$end_terday);
}
/*今天*/
function today() {
$staend = strtotime(date('Y-m-d 00:00:00', time()));
$end = strtotime(date('Y-m-d 23:59:59', time()));
return array($staend,$end);
}
/*本周*/
function week(){
$start_week=mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
$end_week=mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
return array($start_week,$end_week);
}
/*本月*/
function month() {
$beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
$endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
return array($beginThismonth,$endThismonth);
}
/*上个月*/
function prev_month(){
$start_time = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month')));
$end_time = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')));
return array($start_time,$end_time);
}
/*最近七天*/
function lately_week(){
$beginDate = strtotime(date("Y-m-d 00:00:00",strtotime('-7 day')));
$endDate = strtotime(date("Y-m-d 23:59:59",strtotime('-1 day')));
return array($beginDate,$endDate);
}
/*最近上30天*/
function lately_month(){
$beginDate = strtotime(date("Y-m-d 00:00:00",strtotime('-30 day')));
$endDate = strtotime(date("Y-m-d 23:59:59",strtotime('-1 day')));
return array($beginDate,$endDate);
}
/*近半年*/
function half_year(){
$beginDate=strtotime(date('Y-m-01 00:00:00', strtotime("-5 month"))) ;
$endDate=strtotime(date('Y-m-d 23:59:59' , strtotime('-1 day')));
return array($beginDate,$endDate);
}
/*今年*/
function year(){
$start_yan=mktime(0,0,0,1,1,date("Y",time()));
$end_yan=mktime(23,59,59,12,31,date("Y",time()));
return array($start_yan,$end_yan);
}