php - 怎么把时间戳以小时结算
PHP中文网
PHP中文网 2017-04-11 10:05:35
0
4
693

我做了一个活动,能够记录下,开始时间戳,结束时间戳,还有中间的时间差,怎样才能吧这部分时间以小时为单位换算出来

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
伊谢尔伦

看你的时间戳是以什么为单位,秒还是毫秒

得到中间的时间差后,

以秒为单位,就用时间差/3600

以毫秒为单位,就用时间差/3600000

Peter_Zhu

一个小时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.'前';
        }
    }
}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!