简体   繁体   English

在php中以分钟,小时或秒为单位获取两个时间戳之间的差异

[英]Get difference between two timestamps in minutes, hours or seconds in php

I have two timestamps and i would lime to get the difference based on 我有两个时间戳,我会石灰以基于

  1. if its more that 60 seconds get in minutes 如果超过60秒只需几分钟
  2. If more than 60 min get difference in hours 如果超过60分钟,小时数会有所不同

In my code i have 在我的代码中

$diff = ($truckHistory->created_at - $model->created_at);

return $diff 

The above returns in seconds SO i have tried 以上以秒为单位返回了我尝试过的

 $diff = ($truckHistory->created_at - $model->created_at);

 return $this->convertToSecMinHour($diff)

  public function convertToSecMinHour($diff){
    switch($diff){
      case ($diff <= 60):{
            return $diff. "sec"
             break;
          } 

       //stuck for minutes and hours cases
    }


  }

I have this function that does just that, but it includes other methods of gauging time. 我有执行此功能的函数,但是它包含其他度量时间的方法。 You can simply remove those you do not need. 您可以简单地删除不需要的内容。

public function timeAgo($timestamp) {
    $estimateTime = time() - $timestamp;

    if ($estimateTime < 1) {
        return 'less than 1 second ago';
    }

    $condition = array( 
        12 * 30 * 24 * 60 * 60  =>  'year',
        30 * 24 * 60 * 60       =>  'month',
        24 * 60 * 60            =>  'day',
        60 * 60                 =>  'hour',
        60                      =>  'minute',
        1                       =>  'second'
    );

    foreach ($condition as $secs => $str) {
        $d = $estimateTime / $secs;

        if($d >= 1) {
            $r = round( $d );
            return $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
        }
    }
}

You can get quickly using this function just passed the seconds to it. 您只需花几秒钟即可快速使用此功能。 Example here 这里的例子

$time1 = '1504802148'; // Unix timeStamp
$timeDiff = ceil((time() - $time1));

echo secondsToTime($timeDiff);

function secondsToTime($seconds) {
  $dtF = new \DateTime('@0');
  $dtT = new \DateTime("@$seconds");
  $makeTime = $dtF->diff($dtT)->format('%ad,%hh,%im,%ss');
  $data = explode(",", $makeTime);
  $array = array('0d', '0h', '0m');
  $time = array_diff($data, $array);
  return implode("", $time);
}

Use this function: 使用此功能:

<?php
$diff = (time() - time()+60);
$diff2 = (time() - time()+2000);
$diff3 = (time() - time()+5000);

echo convertToSecMinHour($diff)."<br>";
echo convertToSecMinHour($diff2)."<br>";
echo convertToSecMinHour($diff3)."<br>";

function convertToSecMinHour($diff, $decimals = 2) {
    switch($diff){
        case ($diff <= 60):
            $result = $diff;
            $unit = "sec";
            break;
        case ($diff > 60 && $diff < 3600):
            $result =  ($diff / 60);
            $unit = "min";
            break;
        case ($diff >= 3600):
            $result = ($diff / (60 * 60));
            $unit = "hrs";
            break;
        default:
            $result = 0;
            $unit = "";
    }
    return round($result, $decimals).$unit;
}

It'll return difference in seconds (ie 20 sec) if difference is less than 60, difference in minutes (ie 20 min) if difference is between 60 and 3600 seconds, and difference in hours (ie 20 hrs) if difference is larger than 3600. The second parameters is for number of decimals you want in the result (by default, 2). 如果差异小于60,则返回以秒为单位的差异(即20秒);如果差异在60至3600秒之间,则返回分钟的差异(即20分钟);如果差异大于60,则返回小时的差异(即20小时)。 3600。第二个参数用于结果中所需的小数位数(默认为2)。

Demo 演示版

I think you want $result and also you have other variables to use 我认为您想要$ result,并且还有其他变量要使用

$result;
$hour = $diff / 3600;
if ($hour == 0) {
    $min = ($diff % 3600) / 60;
    if ($min == 0) {
        $sec = ($diff % 3600) % 60;
        $result = $sec;
    }else{
        $result = $min;
    }
}else{
    $result = $hour;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM