DEDE
DEDE

Reputation: 3

PHP time add HH:MM:SS

I am trying to add total worked hrs for the employees for the 7 days week period below code is not working for me, not sure what am i doing wrong as I am not a PHP expert, idea please?

<?php 
            $time1 = $row_Roster['sunday_start'];
            $time2 = $row_Roster['sunday_end'];
            list($hours, $minutes, $seconds) = explode(':', $time1);
            $startTimestamp = mktime($hours, $minutes, $seconds);

            list($hours, $minutes, $seconds) = explode(':', $time2);
            $endTimestamp = mktime($hours, $minutes, $seconds);

            $add = $endTimestamp - $startTimestamp;
            if($seconds < 0) {
            $seconds+=60*60*24; 
            }
            $seconds = $add % 60;
            $minutes = ($add / 60) % 60;
            $hours = floor($add / (60 * 60));



            $sunday = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);

            echo $sunday;
            if ($sunday >8)
            echo ('</br><span class="smallred">Long Shift</span>');
?>

does it make sense

    list($hours, $minutes, $seconds) = explode(':', $time1);
    $Timestamp1 = mktime($hours, $minutes, $seconds);
    list($hours, $minutes, $seconds) = explode(':', $time2);
    $Timestamp2 = mktime($hours, $minutes, $seconds);
    list($hours, $minutes, $seconds) = explode(':', $time3);
    $Timestamp3 = mktime($hours, $minutes, $seconds);
    list($hours, $minutes, $seconds) = explode(':', $time4);
    $Timestamp4 = mktime($hours, $minutes, $seconds);

    $add = $Timestamp1 + $Timestamp2 + $Timestamp3 + $Timestamp4;
    if($seconds < 0) {
        $seconds+=60*60*24; 
    }
    $seconds = $add % 60;
    $minutes = ($add / 60) % 60;
    $hours = floor($add / (60 * 60));
    $total = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);             
    echo $total;
?>

Upvotes: 0

Views: 1362

Answers (2)

Rizier123
Rizier123

Reputation: 59681

You make this way to complicated.

First just put all your formatted days into an array ($formattedWorkTimeWeekdays).

Then go through each day with array_map() and calculate the total amount of seconds per day and save it in $secondsWorkTimeWeekdays.

After this you can sum all seconds per day together with array_sum() and you get the total amount of work for this week in seconds in the variable $totalSecondsWorkTimeWeek.

And at the end simply calculate the hours, minutes and seconds out of it.

<?php

    $formattedWorkTimeWeekdays = [
            $monday,
            $tuesday,
            $wednesday,
            $thursday,
            $friday,
            $saturday,
            $sunday,
        ];

    $secondsWorkTimeWeekdays = array_map(function($formattedWorkTimeDay){
        list($hours, $minutes, $seconds) = explode(":", $formattedWorkTimeDay);
        return ($hours * 3600) + ($minutes * 60) + $seconds;
    }, $formattedWorkTimeWeekdays);

    $totalSecondsWorkTimeWeek = array_sum($secondsWorkTimeWeekdays);

    $hoursWorkTimeWeek = $totalSecondsWorkTimeWeek / 3600;
    $minutesWorkTimeWeek = ($totalSecondsWorkTimeWeek % 3600) / 60;
    $secondsWorkTimeWeek = ($totalSecondsWorkTimeWeek % 3600 % 60);

    echo $total = sprintf('%02d:%02d:%02d', $hoursWorkTimeWeek, $minutesWorkTimeWeek, $secondsWorkTimeWeek);

?>

EDIT:

After your edit your have 2 completely different codes, which aren't related to each other at all. For your first revision I gave the answer above.

Now in your edited code you just seem to have 2 dates. You can just create two DateTime objects and get the difference from them, e.g.

$workTimeStart = new DateTime($row_Roster['sunday_start']);
$workTimeEnd = new DateTime($row_Roster['sunday_end']);

$workTimeDifference = $workTimeStart->diff($workTimeEnd);
echo $workTimeDifference->format("%h hours %i minutes %s seconds");

Upvotes: 2

user4035
user4035

Reputation: 23729

Instead of calculations and calling sprintf, you can use gmdate function:

//the same code summing the weekdays seconds
$add = $Timestamp1 + $Timestamp2 + $Timestamp3 + $Timestamp4;
print gmdate("H:i:s", $add);

Upvotes: 0

Related Questions