PHP常用函数之格式化时间操作示例
本文实例讲述了PHP常用函数之格式化时间操作。分享给大家供大家参考,具体如下:
/** *格式化时间 *@param$time时间戳 *@returnbool|string */ functionformatTimeToNow($time){ //获取现在的时间戳 $nowtime=time(); if($time>$nowtime){ return''; }else{ $tc=$nowtime-$time; if($tc>=864000){ $str=date('Y-m-dH:i',$time);//如果大于10天,则直接显示日期 }elseif($tc>=86400){ $str=floor($tc/86400)."天前";//如果大于1天 }elseif($tc>=3600){ $str=floor($tc/3600)."小时前";//如果大于1小时 }elseif($tc>=60){ $str=floor($tc/60)."分钟前";//如果大于1分钟 }else{ $str="刚刚"; } return$str; } }
/** *将中文的日期格式化为正常的日期 *@param$date *@returnmixed */ functionformatCnDateToDate($date){ //把年月替换为-,日替换为空 $date=str_replace('年','-',$date); $date=str_replace('月','-',$date); $date=str_replace('日','',$date); //避免提交的格式不统一,例如2018-3-2等,标准化 returndate('Y-m-d',strtotime($date)); }
/** *计算自然周期的开始时间戳和结束时间戳(周一到周日,月初到月末) *@paramint$time_type1表示自然天,2表示自然周,3表示自然月 *@paramint$prev_num距离现在的值(前一周传-1,前两周传-2...) *@returnarray|bool */ functionnaturalFormatTime($time_type=1,$prev_num=0){ $today_start_time=strtotime(date('Y-m-d00:00:00',time()));//今天0点的时间戳 if($time_type==1){ if($prev_num==0){ returnarray('start_time'=>$today_start_time,'end_time'=>time(),'show_date'=>date('Y年m月d日',time())); }elseif($prev_num<0){ $start_time=$today_start_time-86400*abs($prev_num); $end_time=$start_time+86399; $show_date=date('Y年m月d日',$start_time); returnarray('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date); }else{ returnfalse; } }elseif($time_type==2){ $today_week=date('w',$today_start_time); if($today_week==0){ $today_week_start_time=$today_start_time-86400*6; }else{ $today_week_start_time=$today_start_time-86400*($today_week-1); } if($prev_num==0){ $show_date=date('Y年m月d日',$today_week_start_time); $show_date.='至'.date('d日',time()); returnarray('start_time'=>$today_week_start_time,'end_time'=>time(),'show_date'=>$show_date); }elseif($prev_num<0){ $start_time=$today_week_start_time-86400*7*abs($prev_num); $end_time=$start_time+(86400*7-1); $show_date=date('Y年m月d日',$start_time); $show_date.='至'.date('d日',$end_time); returnarray('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date); }else{ returnfalse; } }elseif($time_type==3){ if($prev_num==0){ $today_day=ltrim(date('d',$today_start_time),0); $today_month_start_time=$today_start_time-86400*($today_day-1); $show_date=date('Y年m月d日',$today_month_start_time); $show_date.='至'.date('d日',time()); returnarray('start_time'=>$today_month_start_time,'end_time'=>time(),'show_date'=>$show_date); }elseif($prev_num<0){ $start_time=strtotime(date('Y-m-01',strtotime("$prev_nummonth"))); $days=date('t',$start_time); $end_time=$start_time+86400*$days-1; $show_date=date('Y年m月d日',$start_time); $show_date.='至'.date('d日',$end_time); returnarray('start_time'=>$start_time,'end_time'=>$end_time,'show_date'=>$show_date); }else{ returnfalse; } }else{ returnfalse; } }
/** *计算近一周或近一个月的开始时间戳和结束时间戳 *@param$type1表示今天,2表示近一周,3表示近一个月 *@returnarray */ functionnearFormatTime($type){ $start_time=strtotime(date('Y-m-d00:00:00'));//今天0点的时间戳 $end_time=$start_time+86399;//今天23:59的时间戳 $res=array('start_time'=>0,'end_time'=>$end_time); if($type==1){ //今天 $res['start_time']=$start_time; }elseif($type==2){ //近一周 $res['start_time']=$start_time-86400*6;//包括今天,共七天 }elseif($type==3){ //近一个月 $res['start_time']=$start_time-86400*30;//包括今天,共31天 } return$res; }
PS:这里再为大家推荐几款时间及日期相关工具供大家参考:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc
在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。