Geoff
Geoff

Reputation: 6629

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
    }


  }

Upvotes: 0

Views: 1303

Answers (4)

sadegh
sadegh

Reputation: 43

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

Upvotes: 0

ishegg
ishegg

Reputation: 9927

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 (i.e. 20 sec) if difference is less than 60, difference in minutes (i.e. 20 min) if difference is between 60 and 3600 seconds, and difference in hours (i.e. 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

Upvotes: 0

Muhammad Usman
Muhammad Usman

Reputation: 1423

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

Upvotes: 0

user2928301
user2928301

Reputation: 87

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

Upvotes: 2

Related Questions