2

I am building an online timesheet for employees. At the end of the timesheet I need to show the total hours:mins worked.

I have the hours worked as variables:

$MondaysActual 
$TuesdaysActual 
$WednesdaysActual 
$ThursdaysActual 
$FridaysActual 

The data held is something like 07:20 - the employee worked 7 hours 20 mins

How can I simply add these 5 times together and show the total as hrs:mins?

5
  • 1
    Could you please specify what have you tried and why it wasn't working for you? Commented May 2, 2018 at 16:21
  • stackoverflow.com/questions/22681725/… - probably duplicate Commented May 2, 2018 at 16:26
  • 1
    @Alex - Tried this but the result returns nothing Commented May 2, 2018 at 16:35
  • What exactly you've tried (codebase)? What the result you've got? What the errors were? What the specific env you use? The more specific question is - the simpler is to find the answer. Also, consider review stackoverflow.com/help/how-to-ask Commented May 2, 2018 at 16:46
  • 1
    from what we can see, you have tried to enter 5 variable names in some unspecified text editor, and posted that here. You have not tried coding to solve your requirement. Commented May 2, 2018 at 17:02

1 Answer 1

2

Try this

   $times = array();


$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";

// pass the array to the function
echo AddPlayTime($times);

function AddPlayTime($times) {
    $minutes = 0; //declare minutes either it gives Notice: Undefined variable
    // loop throught all the times
    foreach ($times as $time) {
        list($hour, $minute) = explode(':', $time);
        $minutes += $hour * 60;
        $minutes += $minute;
    }

    $hours = floor($minutes / 60);
    $minutes -= $hours * 60;

    // returns the time already formatted
    return sprintf('%02d:%02d', $hours, $minutes);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.