Heim  >  Fragen und Antworten  >  Hauptteil

php - 怎么把时间戳以小时结算

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

PHP中文网PHP中文网2731 Tage vor704

Antworte allen(4)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-04-11 10:07:35

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

    得到中间的时间差后,

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

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

    Antwort
    0
  • 天蓬老师

    天蓬老师2017-04-11 10:07:35

    一个小时3600秒,那么5000秒是多少个小时呢?

    Antwort
    0
  • 大家讲道理

    大家讲道理2017-04-11 10:07:35

    这个简单,时间差/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.'秒';

    Antwort
    0
  • PHP中文网

    PHP中文网2017-04-11 10:07:35

    可以参考下这个函数

    /**
     * [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.'前';
            }
        }
    }
    
    

    Antwort
    0
  • StornierenAntwort