时间:2021-07-01 10:21:17 帮助过:3人阅读
①根据两个时间戳计算时间差
/* *计算时间差函数 */ function timediff($begin_time,$end_time) { if($begin_time < $end_time){ $starttime = $begin_time; $endtime = $end_time; }else{ $starttime = $end_time; $endtime = $begin_time; } //计算天数 $timediff = $endtime-$starttime; $days = intval($timediff/86400); //计算小时数 $remain = $timediff%86400; $hours = intval($remain/3600); //计算分钟数 $remain = $remain%3600; $mins = intval($remain/60); //计算秒数 $secs = $remain%60; $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs); return $res; }②根据两个时间戳计算相差的天数 ----timediff为上一个函数
function dayTime($begin_time,$end_time,$controller = null){ date_default_timezone_set('Asia/Shanghai'); //设置时区,不然就用默认的 $timediff = timediff($begin_time,$end_time); $totalTime = floatval(($timediff['sec']+($timediff['min']*60)+($timediff['hour']*3600)+($timediff['day']*3600*24))/3600); if($controller == null){ echo round($totalTime, 2); //保留小数点两位 }else{ return round($totalTime, 2); } }③根据两个时间戳判断是否同一天----timediff为上一个函数
/* * 判断是否一年中的同一天 */ function sameDay($begin_time,$end_time){ //传入时间戳 if(date("z",$begin_time) != date("z",$end_time)){ //非一年中的同一天 return false; }elseif (date("z",$begin_time) == date("z",$end_time)) { //是一年中的同一天 return true; } }
以上就介绍了PHP部分时间函数总结,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。