View Single Post
Old 10-07-2012, 03:07 AM   PM User | #3
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
The function below should be OK. Some of the results are different than yours so it might need fine tuning.

PHP Code:
<?php

function elapsed_time($seconds$precision 2) {

$a = array('decade' => 315576000'year' => 31557600'month' => 2629800'week' => 604800'day' => 86400'hour' => 3600'min' => 60'sec' => 1);
  
$i 0;
    foreach(
$a as $k => $v) {
      $
$k floor($seconds/$v);
      if ($
$k$i++;
      
$seconds $i >= $precision $seconds - $$k $v;
      
$s = $$k 's' '';
      $
$k = $$k ? $$k.' '.$k.$s.' ' '';
      @
$result .= $$k;
    }

return 
$result;
}


$hours=13;
$seconds =($hours*3600);
echo 
elapsed_time($seconds6).'<br />';

$hours=982;
$seconds =($hours*3600);
echo 
elapsed_time($seconds6).'<br />';

$hours=145;
$seconds =($hours*3600);
echo 
elapsed_time($seconds6).'<br />';

$hours=200;
$seconds =($hours*3600);
echo 
elapsed_time($seconds6).'<br />';

?>

Ouput
Code:
13 hours
1 month 1 week 3 days 11 hours 30 mins
6 days 1 hour
1 week 1 day 8 hours
__________________
Leonard Whistler

Last edited by Len Whistler; 10-07-2012 at 04:30 AM..
Len Whistler is offline   Reply With Quote