84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
我做了一个活动,能够记录下,开始时间戳,结束时间戳,还有中间的时间差,怎样才能吧这部分时间以小时为单位换算出来
认证高级PHP讲师
看你的时间戳是以什么为单位,秒还是毫秒
得到中间的时间差后,
以秒为单位,就用时间差/3600
以毫秒为单位,就用时间差/3600000
一个小时3600秒,那么5000秒是多少个小时呢?
这个简单,时间差/3600,向下取整,就是多少小时,然后总减去最大小时,除以60,就是多少分钟,向下取整,剩下的就是秒
$start=1488484037; $end=1488504037; $difference=$end-$start; //小时 $h=intval($difference/3600); $i=intval(($difference-$h*3600)/60); $s=$difference-$h*3600-$i*60; echo '耗时'.$h.'小时,'.$i.'分,'.$s.'秒';
可以参考下这个函数
/** * [format_date 格式化时间] * @param [type] $start_time [开始时间] * @param [type] $end_time [结束时间] * @return [type] [string] */ function format_date($start_time, $end_time){ $t = $end_time - $start_time; $f = [ // '31536000' => '年', // '2592000' => '个月', // '604800' => '星期', // '86400' => '天', '3600' => '小时', // '60' => '分钟', // '1' => '秒' ]; foreach ($f as $k=>$v) { if (0 != $c = floor($t/(int)$k)) { return $c.$v.'前'; } } }
看你的时间戳是以什么为单位,秒还是毫秒
得到中间的时间差后,
以秒为单位,就用时间差/3600
以毫秒为单位,就用时间差/3600000
一个小时3600秒,那么5000秒是多少个小时呢?
这个简单,时间差/3600,向下取整,就是多少小时,然后总减去最大小时,除以60,就是多少分钟,向下取整,剩下的就是秒
可以参考下这个函数