1

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.

morgyface
  • 368
  • 1
  • 9

2 Answers2

4
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'); 

    $return_string = "";

    if ( $duration_hours != '0' ) {
        $return_string = $return_string . $duration_hours . 'hrs '; 
    }
    if ( $duration_mins != '0' ) {
        $return_string = $return_string . $duration_mins . 'min';
        if ( $duration_mins > '1' ) { 
            $return_string = $return_string .  's';
        }        
    }

    return $return_string;

}

Or you can use $return_string .= ___ instead of $return_string = $return_string . ___

Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
1

Keyword you are looking for is Return

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'); 
    $returnValue = "";
    if ( $duration_hours != '0' ) {
        $returnValue += $duration_hours . 'hrs '; 
    }
    if ( $duration_mins != '0' ) {
        $returnValue += $duration_mins . 'min';
        if ( $duration_mins > '1' ) { 
            $returnValue += "s";
        }        
    }

return $returnValue; }

connormcwood
  • 323
  • 2
  • 12
  • 1
    The `+=` is not used for strings. I think you meant `.=`. – Thanasis1101 Aug 11 '17 at 11:28
  • This is brilliant, now the two of you have mentioned `+=` and `.=` I've been able to find out about **assignment operators**, which is exactly what I was looking for, a way to build a final result and then **return** as oppose to **echo**. Thank you @connormcwood and @Thanasis, you have educated me and I'm grateful! – morgyface Aug 11 '17 at 11:59
  • 1
    @morgyface You should mark Thanasis's answer as the accepted question. In this case his was correct, and what you asked for. Keep it going! – connormcwood Aug 11 '17 at 12:20