Time calculation

This example shows how you can use custom PHP code to perform calculations based on elapsed time.

    Start time:
    :

    End time:
    :

    Hours:

    Hours between midnight and 9:00

    Base price:

    30 € per hour

    Overtime supplement:

    25% extra between midnight and 9:00 AM.

    Discount (%):
    %
    10% discount applies for 8 hours or more.

    Total:

    Source code for this form:

    Start time:
    [select start_h "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23"] : [select start_m "00" "15" "30" "45"]
    
    End time:
    [select end_h "00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23"] : [select end_m "00" "15" "30" "45"]
    
    [calculate_button my_calculate_button "Calculate"]
    
    Hours:
    [calculation hours min:0 precision:2 "fn_php($elapsed = strtotime($_POST{'end_h'} . ':' . $_POST{'end_m'}) - strtotime($_POST{'start_h'} . ':' . $_POST{'start_m'}); return $elapsed >= 0 ? $elapsed : 86400 + $elapsed;) / 3600"]
    
    Hours between midnight and 9:00
    [calculation hours_after_midnight min:0 precision:2 "fn_php(
    $cutoff = 9;
    if ($_POST{'start_h'} < $cutoff) {
        $r = min(
            $cutoff * 3600,
            min(
            	strtotime($cutoff . ':00'),
            	strtotime($_POST{'end_h'} . ':' . $_POST{'end_m'})
            ) - strtotime($_POST{'start_h'} . ':' . $_POST{'start_m'})
        );
    } elseif ($_POST{'start_h'} > $_POST{'end_h'}) {
        $r = min(
            $cutoff * 3600,
            (int) $_POST{'end_h'} * 3600 + (int) $_POST{'end_m'} * 60
        );
    } else {
        $r = 0;
    }
    return $r / 3600;
    )"]
    
    Base price:
    [calculation base_price min:0 precision:2 "hours * 30"] €
    <small><em>30 € per hour</em></small>
    
    Overtime supplement:
    [calculation overtime min:0 precision:2 "hours_after_midnight * 30 * 0.25"] €
    <small><em>25% extra between midnight and 9:00 AM.</em></small>
    
    Discount (%):
    [calculation discount min:0 precision:0 "fn_php(return ($_POST{'hours'} >= 8) ? 10 : 0;)"] %
    <small><em>10% discount applies for 8 hours or more.</em></small>
    
    Total:
    [calculation total min:0 precision:2 "(base_price + overtime) * (1 - discount / 100)"] €