My database has a start timestamp and an end timestamp. Although I've not yet had much experience with functions, I've created a function which basically outputs a duration in hours and minutes, that is, the difference between the start and end.
It all works, but it is printing automatically, what I'd like to achieve is the ability to store the result as a variable and echo it.
Here is the function:
function session_duration($start, $end) {
$date_start = new DateTime($start); //start time
$date_end = new DateTime($end); //end time
$interval = $date_start->diff($date_end);
$duration_hours = $interval->format('%H');
$duration_mins = $interval->format('%i');
if ( $duration_hours != '0' ) {
echo $duration_hours . 'hrs ';
}
if ( $duration_mins != '0' ) {
echo $duration_mins . 'min';
if ( $duration_mins > '1' ) {
echo 's';
}
}
}
So wherever I place session_duration($start, $end)
on the page, the duration is shown, what I'd like to be able to do is:
$duration = session_duration($start, $end);
echo 'Duration: ' . $duration;
I suppose what I'm looking for is the advice on how to, almost, build an end return, a compilation of the output that isn't echoed within the function but I'm lacking the knowledge. Any help would be gratefully received.