简体   繁体   中英

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
  2. If more than 60 min get difference in hours

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).

Demo

I think you want $result and also you have other variables to use

$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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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