Sum of hours

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
NerdHerd777
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Nov 02, 2018 2:49 am

Fri Nov 02, 2018 3:02 am

I have a column that has the number of hours worked in the following format eg: 03:00 which means the person worked 3 hours.

I do not seem to able to get them to add up and give the total in the same format: eg Person 1 worked for 02:00 hours on Monday and 03:00 hours on Tuesday and therefore has worked for 05:00 in that particular week. The code in the report is

Code: Select all

 echo '<td>'. $row['hours_worked'] . '</td>';
Among the many things I have tried - and failed - here is one of them:

Code: Select all

 $count_hours = $pdo->query("SELECT( SUM(`hours_worked` ))FROM database WHERE member='Joe' ")->fetchColumn(); 
I then echo $count_hours, but the only answer I get is 0 no matter how many hours have been worked by Joe

I am still new to PHP and would appreciate any help or advice in this small problem

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 747
Joined: Mon Feb 22, 2016 5:52 pm

Fri Nov 02, 2018 10:45 am

You need to translate that format to something that PHP and or your database can understand and can work with, that either means creating functions to perform the translation or use the built in date/time format instead.

Or to put it another way, '03:00' is just a string in PHP, it doesn't understand the colon as a numeric separator and concludes that it is a string which it cannot use in calculations.

Have a look here, for data types (even if you are not using MySQL)

NerdHerd777
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Nov 02, 2018 2:49 am

Thu Nov 08, 2018 1:11 am

Thank you for the information, I found it informative. I understand the concept and have read the linked article but am still failing miserably at getting the correct totals.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 747
Joined: Mon Feb 22, 2016 5:52 pm

Sun Nov 11, 2018 12:23 pm

What changes have you made to your code now that you have read about the differences between data types and how you can manipulate the different data types?

Post Reply