Subtracting time

I am trying to subtract time

  <?php
            $start_time = date('09:00:00');
            $close_time = date('18:00:00');
        $query = "SELECT * FROM staff_attend WHERE staff_name= '$staffname'";
        $result_data = mysqli_query($conn, $query) or die('Unable to execute' . mysqli_error($conn));
        while ($row_data = mysqli_fetch_assoc($result_data)) {
            $rep_time = strtotime($row_data['rep_time']);
            $leave_time=strtotime($row_data['leav_time']);
            $start=(strtotime($start_time));
            $leave=(strtotime($close_time));
            $morning_shift=(($rep_time-$start)/3600);
            $evening_shift=(($leave-$leave_time)/3600);
            echo "<tr>";
            echo "<td class='tg-h2xt80'>".date('d/m/Y',strtotime($row_data['comp_date']));
            echo "<td class='tg-h2xt120'>".$row_data['rep_time'];
            echo "<td  class='tg-h2xt80'>". $row_data['leav_time']."</td>";
            echo "<td  class='tg-h2xt120'>".$morning_shift;
            echo "<td  class='tg-h2xt120'>".$evening_shift;

if a person is late by 1 hr it shows 1 , but
if a person is late bu 30 minutes it shows 0.5 and not 0.30
or
if late by 1.05 it shows 1.08333
how do i correct this ?

$start_time = strtotime('09:00:00');
$close_time = strtotime('18:00:00');

$query = "SELECT * FROM staff_attend WHERE staff_name = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "s", $staffname);
mysqli_stmt_execute($stmt);
$result_data = mysqli_stmt_get_result($stmt);

while ($row_data = mysqli_fetch_assoc($result_data)) {
    $rep_time = strtotime($row_data['rep_time']);
    $leave_time = strtotime($row_data['leav_time']);

    $morning_shift = gmdate("H:i:s", $rep_time - $start_time);
    $evening_shift = gmdate("H:i:s", $close_time - $leave_time);

    echo "<tr>";
    echo "<td class='tg-h2xt80'>".date('d/m/Y', strtotime($row_data['comp_date']))."</td>";
    echo "<td class='tg-h2xt120'>".$row_data['rep_time']."</td>";
    echo "<td class='tg-h2xt80'>".$row_data['leav_time']."</td>";
    echo "<td class='tg-h2xt120'>".$morning_shift."</td>";
    echo "<td class='tg-h2xt120'>".$evening_shift."</td>";
    echo "</tr>";
}

thanks much appreciated :grinning:

I got the timing correct. But when I multiply
echo “”.$time_hr*$salary_hourly."";
I get the following error " A non-numeric value encountered in…"
$time_hr is a string and $salary_hourly is double.
If I convert time_hr it converts into an integer.
so a time of 1.15 shows as 1, or 2.56 shows as 2

        <?php
        $start_time = strtotime('09:00:00');
        $close_time = strtotime('18:00:00');
        $query = "SELECT * FROM staff_attend WHERE staff_name= '$staffname'";
        $result_data = mysqli_query($conn, $query) or die('Unable to execute' . mysqli_error($conn));
        while ($row_data = mysqli_fetch_assoc($result_data)) {
            $rep_time = strtotime($row_data['rep_time']);
            $leave_time = strtotime($row_data['leav_time']);
            $morning_shift = $rep_time - $start_time;
            $evening_shift = $close_time - $leave_time;
            $time_hr = gmdate("H:i",doubleval($morning_shift + $evening_shift));
            //$xyz = explode(":", $time_hr);
            //$xyz = ($xyz[1]);
            echo "<tr>";
            echo "<td  class='tg-h2xt80'>".date('d/m/Y', strtotime($row_data['comp_date']));
            echo "<td class='tg-h2xt120'>".$row_data['rep_time'];
            echo "<td  class='tg-h2xt80'>".$row_data['leav_time'] . "</td>";
            echo "<td class='tg-h2xt120'>".gmdate("H:i",$morning_shift)."</td>";
            echo "<td class='tg-h2xt120'>".gmdate("H:i",$evening_shift)."</td>";
            echo "<td class='tg-h2xt120'>".$time_hr."</td>";
            echo "</tr>";
        }
        ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service