Duration calculation
The following funtion is used to calculate the duration with years, weeks, days, hours, minutes and seconds.
<?php
function calculate_duration($timestamp) {
$years=floor($timestamp / (60*60*24*365));
$timestamp%=60*60*24*365;
$weeks=floor($timestamp / (60*60*24*7));
$timestamp%=60*60*24*7;
$days=floor($timestamp / (60*60*24));
$timestamp%=60*60*24;
$hrs=floor($timestamp / (60*60));
$timestamp%=60*60;
$mins=floor($timestamp / 60);
$secs=$timestamp % 60;
$output=”";
if ($years >= 1) { $output.=”{$years} years “; }
if ($weeks >= 1) { $output.=”{$weeks} weeks “; }
if ($days >= 1) { $output.=”{$days} days “; }
if ($hrs >= 1) { $output.=”{$hrs} hours “; }
if ($mins >= 1) { $output.=”{$mins} minutes “; }
if ($secs >= 1) { $output.=”{$secs} seconds “; }
return $output;
}
echo calculate_duration(368); // returns “6 minutes 8 seconds”
?>
In above example, 368 is the input parameter which is passed to calculate_duration() function. Inside the funtion, we are calculating years, weeks, days, hours, minutes and seconds for the passed input and returning the output.
