-1

I have this, but it can only display time difference in seconds or minutes or hours.

$diff = strtotime('2015-02-10 17:03:44') - strtotime(''.$row['start_time'].'');
$mins = $diff / 60;
$hrs = $mins / 60;
echo $diff." seconds<br />";
echo $mins." minutes<br />";
echo $hrs." hours<br />";

The first datetime (the end_time) is static, but will be dynamic later. Just doing it this way for testing. The start_time in this example is: 2015-02-10 17:03:14

The result of the above is:

30 seconds
0.5 minutes
0.0083333333333333 hours

Instead of showing the total difference in seconds and the total difference in minutes and the total difference in hours, I would like to show the total difference in hours, minutes and seconds, like this:

0 hours 0 minutes 30 seconds

If hours is less than 1 it should be rounded to 0, and the same for minutes. If the difference had minutes and seconds then an example of that would be:

0 hours 5 minutes 45 seconds

If the difference had all 3 then this would be an example result:

1 hour 17 minutes 10 seconds

How can I do that?

leoarce
  • 567
  • 2
  • 8
  • 33
  • Did you try [this solution](http://stackoverflow.com/a/18602474/67332)? – Glavić Feb 11 '15 at 05:55
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – Glavić Feb 11 '15 at 05:55
  • not a duplicate of those. more like duplicate of this one, which worked for me: http://stackoverflow.com/a/11160373/2827550 – leoarce Feb 11 '15 at 17:22

1 Answers1

0

Here is the solution :

$diff = "";
    if( @$params["from_date"] && @$params["to_date"] )
    {
        $days = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params ['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->d );
        $months = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->m );
        $years = intVal( DateTime::createFromFormat( "d-m-Y H:i:s", $params['from_date']." 00:00:00" )->diff( DateTime::createFromFormat("d-m-Y H:i:s", $params['to_date']." 00:00:00" ) )->y );
        // for years, months and days
        if($months == 0 && $years == 0 && $days >= 1) 
            $diff = $days. ' day(s)';
        elseif($months == 0 && $years >= 1 && $days == 0)  
            $diff = $years.' year(s)';
        elseif($months == 0 && $years >= 1 && $days >= 1)
            $diff = $years.' year(s) '. $days.' day(s)';
        elseif($months >= 1 && $years == 0 && $days == 0)
            $diff = $months.' month(s)';
        elseif($months >= 1 && $years == 0 && $days >= 1)
            $diff = $months.' month(s) '.$days.' day(s)';
        elseif($months >= 1 && $years >= 1 && $days == 0)
            $diff = $years. ' year(s) '.$months.' month(s) ';
        elseif($months >= 1 && $years >= 1 && $days >= 1)
            $diff = $years.' year(s) '.$months.' month(s) '.$days.' day(s)';
        else
        {   
            $diff = $years.' year(s) '.$months.' month(s) ';
        }
    }
    echo $diff;
Jaskaran Singh
  • 2,392
  • 24
  • 39
  • couldn't get this to work. wen't with different solution located here: http://stackoverflow.com/a/11160373/2827550 – leoarce Feb 11 '15 at 17:20