My elapsed time code not working

Hi there,
I found this piece of code that suppose to gives the elapsed time of opening page but it dose’t do . How could I bring it to execute that?
Here the code $time = strtotime(date(‘Y-m-d h:i:s a’));
echo ‘This was ‘.humanTiming($time).’ ago’ . ‘
’;
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
604800 => ‘w’,
86400 => ‘d’,
3600 => ‘h’,
60 => ‘min’,
1 => ‘s’
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.’ ‘.$text.(($numberOfUnits>1)?‘s’:’’);
}
}

Well, this is an odd way to handle time. You might want to use ‘time objects’ which allow you to pull the elapsed time in one simple command. Something, loosely, like this might work:

$start_time  = date('Y-m-d h:i:s a');
...  do some processing here ....
$end_time = date('Y-m-d h:i:s a');
$diff = $start_time->diff( $end_time );
echo "Processing time: " . $diff->format( '%H:%I:%S' );

Not tested, and this just gives hours-minutes-seconds, but, can be adjusted to show days and weeks too.
Seldom do you see anyone who runs a process that takes days or weeks… (Although I have some!)

Sponsor our Newsletter | Privacy Policy | Terms of Service