how to get sum of the total time in php - php

Currently i working with the attendance management system.i calculate how many hours work done in employees.i already calculate the how much hours working in day and it store in the mysql database.
$totaltime = (strtotime($time_out) - strtotime($time_in));
$hours = sprintf('%02d', intval($totaltime / 3600));
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = sprintf('%02d', intval($seconds_remain / 60));
$seconds = sprintf('%02d' ,($seconds_remain - ($minutes * 60)));
$final = '';
if ($time_in == '' || $time_out == '')
{
$final = '';
}
else
{
$final .= $hours.':'.$minutes.':'.$seconds;
}
for example
$time_in = 08:09:57
$time_out = 16:04:50
$final = 07:54:53 (total working hours)
now i want to get the current month total working time for each employee.how do get sum of the $final using php?
sample data of the month_data
Emp_no Date Time_in Time_out Total_hours TranID
23 2019-08-01 07:54:40 16:01:40 08:07:00 1
23 2019-08-02 07:42:35 16:02:53 08:20:18 2
i want get the sum of the Total_hours for related one employee

If you ask me this can be easily done using plain MySQL, no meed for PHP to calculate this.
You could take a look at a query somewhat like this
SELECT SEC_TO_TIME(SUM(`Total_hours`) ) FROM `month_data` GROUP BY `Emp_no`;
there is a simple SUM function which can do this for you, it returns the total time in seconds though.
In order to turn that into readable time you can use the MySQL function SEC_TO_TIME.
edit
If the said column is not a TIME column you can CAST it to be handled as this type of column using CAST() the needed SQL would look something like
SELECT SEC_TO_TIME(SUM(CAST(`Total_hours` AS TIME)) ) FROM `month_data` GROUP BY `Emp_no`;
My suggestion would be to change the column type to TIME though.
edit 2
I was under the assumption that SUM() would be smart enough to convert the time to seconds and come up with the correct sum of the given times.
Not sure why yet but this is not the case, therefore you need to convert the given times to seconds first.
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`Total_hours`)) ) FROM `month_data` GROUP BY `Emp_no`;
Now I have not tested this but TIME_TO_SEC() seems to accept VARCHAR just fine so need to CAST() the column anymore.

take a look at this:
echo OverallTime($allTimes);
$allTimes = array();
function OverallTime($allTimes) {
$minutes = 0;
foreach ($allTimes as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
return sprintf('%02d:%02d', $hours, $minutes);

<?php
$total = [
'00:02:55',
'00:07:56',
'01:03:32',
'01:13:34',
'02:13:44',
'03:08:53',
'03:13:54'
];
$sum = strtotime('00:00:00');
$sum2=0;
foreach ($total as $v){
$sum1=strtotime($v)-$sum;
$sum2 = $sum2+$sum1;
}
$sum3=$sum+$sum2;
echo date("H:i:s",$sum3);
?>

In case this is useful to someone looking for this, this is what I use on my music website. This code gets the duration in seconds of all the songs in an album, adds them up, and returns the total album length in hh mm ss.
$record_id = $this->record->id; <!--variable for record-->
.$query = 'SELECT SUM(duration) FROM #__songs WHERE `record_id` = '. $db->quote( (int) $record_id ); <!--selects the duration of all the songs in the album-->
$db->setQuery($query);
$results = $db->loadResult();
echo gmdate("H:i:s", $results); <!--echo total time in hh mm ss.-->
Not an expert here. If you see something, say something XD

Related

Sum of Duration from Database Field (Duration)

I am having issue in SUM of Time or Duration from my Database field.
Suppose. I have 5 Records
1. 00:25:32
2. 00:08:52
3. 00:33:22
4. 00:25:30
5. 00:15:12
how to sum all the times with php.
I spent a lot time on it but failed.
I have tried this.
$time = "00:58:30";
$time2 = "00:12:35";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
echo $result;
But it not works good for multiple records
Thanks to all of you. I have fixed it. I checked some other question on Stackoverflow and i got answer what i wanted.
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`SongDuration`))) AS `TimeSum` FROM `mu_forms_competition_entries`
Durations are not dates. What would tomorrow at 35:00:00 mean?
The closest builtin feature is DateInterval, but in order to use it as-is your input data needs some rewriting ('00:25:32' into 'PT00H25M32S') and I don't think it supports addition anyway.
It isn't difficult to make the calculations yourself (error checking omitted and negative times not considered for brevity):
function timeToSeconds(string $time): int
{
[$h, $m, $s] = explode(':', $time);
return $s + 60 * $m + 3600 * $h;
}
function secondsToTime(int $seconds): string
{
return sprintf(
"%02d:%02d:%02d",
floor($seconds / 3600),
floor(($seconds / 60) % 60),
$seconds % 60
);
}
$times = [
'00:25:32',
'00:08:52',
'00:33:22',
'00:25:30',
'00:15:12',
];
$total = 0;
foreach ($times as $time) {
$total += timeToSeconds($time);
}
echo secondsToTime($total);
If you're allowed to change the database query there's probably a builtin function to convert to seconds right from SQL. For instance, MySQL has TIME_TO_SEC().
Last but not least, if your database actually has a Time column type, perhaps it allows to SUM() it.

PHP - How to deduct a time to a total time without converting to decimal?

I'm currently working with a timekeeping system which computes the sum of the basic hours of the week and deduct certain time if there's a late record.
Given that the employee has a total hours rendered for this week is 45 hours (45:00), and he she/has a total late record for that week of 50 minutes (00:50),
Using, PHP. How can I deduct the late record to the total hours rendered without converting time to decimal? The desired output for the above sample is 44:10 since 00:50 is deducted to 45:00.
I see so your goal is to subtract durations ex.
45:00 - 00:50 = 44:10
1: Create a function that convert them into hours
function convertToHours($duration) {
$duration = explode(':',$duration);
$hours+= (int)$duration[0];
$hours+= (int)$duration[1] / 60;
return $hours;
}
2: Create a funciton thats convert from seconds to duration hours:seconds
function secondsToDuration($seconds) {
$H = floor($seconds / 3600);
$i = ($seconds / 60) % 60;
$s = $seconds % 60;
return sprintf("%02d:%02d:%02d", $H, $i, $s);
}
Convert them into hours using function created
$duration1 = convertToHours("25:00");
$duration2 = convertToHours("00:50");
Then subtract them
$difference = $duration1 - $duration2;
Lastly use the created method which convert them back into duration
$duration = secondsToDuration($difference * 3600);
See Demo here
Hope it helps you
You can convert the string to a date and get the difference.
$d1 = "00:45:00";
$d2 = "00:00:50";
date_default_timezone_set("utc");
$fakedate = '01/01/2017';
$d1 = $fakedate . ' ' . $d1;
$d2 = $fakedate . ' ' . $d2;
$dt1 = new DateTime($d1);
$dt2 = new DateTime($d2);
$diff = $dt1->diff($dt2);
echo $diff->format("%H:%I:%S");
The output will be: 00:44:10

Build array from query for a total time calculation

I have this code that draws the data from the db, that when run displays the time totals for each record in a hh:mm format, eg 00:35 for 35 minutes.
$query="SELECT * FROM data1 ORDER BY id DESC";
$result=mysql_query ($query);
$num=mysql_num_rows ($result);
mysql_close();
$i=0;
while ($i < $num) {
$time_ttl=mysql_result($result,$i,"time_ttl");
echo $time_ttl . "<br>";
++$i;
}
The result from the query looks like this:
00:35
00:25
00:10
Total time: 01:10 (1 hour 10 minutes)
Depending on the query, the number of totals will vary, and I want to calculate a grand total depending on the query.
I tried using this code to calculate the time, which works, but I am not sure how to connect the incoming data inside the 'while { }' part:
$times = array();
$times[] = "00:35";
$times[] = "00:25";
$times[] = "00:10";
function ttl_time($times) {
foreach ($times as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}
$hours = floor($minutes / 60);
$minutes -= $hours * 60;
$ttl_time = sprintf('%02d:%02d', $hours, $minutes);
}
The code calculates the total time correctly, but I am looking for guidance how to build an array based on the query to feed into the ttl_time function so I can connect the incoming query data to the function.
I did try this:
$result2 = mysql_query("SELECT sum(time_ttl) FROM data1");
while ($rows = mysql_fetch_array($result2)) {
echo $rows['sum(time_ttl)'];
}
But it returns the wrong result
If I understand your question correctly...
$i=0; $result_times = array();
while ($i < $num) {
$result_times[] = mysql_result($result,$i,"time_ttl");
++$i;
}
Then you can pass $result_times to your ttl_time function.
As for your other attempted solution (using sum) it depends on the data type. This may be helpful: calculate a sum of type time using sql

Averaging times in PHP that are returned from MySQL query

I have 3 times returned from a query in the time format they are:
00:31:36
00:32:58
00:11:59
I want to get an average of these times in the same format.
currently I am trying to use
date('H:i:s', array_sum(array_map('strtotime', $results->[6])) / count($results->[6]))
But the output I am getting is 19:00:00 If I increase the output to 'm/d/y H:i:s' I am getting 12/31/69 19:00:00. If I delete the 'strtotime', I still get the same output.
Thanks for your help
Instead of relying on PHP to perform the AVG aggregation, instead use the DB where it is much more efficient:
SELECT sec_to_time(avg(time_to_sec(<time>))) from <table>
Solution that works for me
<?php $totalTime = 0;
$sql = 'SELECT * FROM ' . TABLE_HTA . ' WHERE date_time >= curdate() AND employee_id = "4104" ORDER BY order_id ASC';
$orders = $db->Execute($sql);
if($orders->RecordCount() > 0) {
while(!$orders->EOF) {
$totalRuns = $totalRuns + 1;
$str_time = $orders->fields['otd_time'];
$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;
$orders->MoveNext();
}
}
$otdAverage = $totalTime / $totalRuns;
echo gmdate("H:i:s", $otdAverage);?>
input was:
00:31:36
00:32:58
00:11:59
00:22:52
00:32:08
00:22:19
00:51:46
The output was 00:29:22
phpMyAdmin sql query output was 00:29:22
So a difference of 1 second due to rounding somewhere. Thanks for your help. Hope this helps someone else!

mysql convert decimal time to xx:xx format

I am trying to convert a decimal time into an actual time format with hours and minutes, ie: in xx:xx hours.
My query is:
select SUM(vt.vluchtdec) AS vluchttijddecimal
from tbl_vluchtgegevens vg
left join tbl_vluchttijd vt
on vg.vluchttijddec = vt.vluchttijdID
WHERE vg.vertrekdatum <=NOW();
And I am echoing
. $row['vluchttijddecimal'] .
I have also tried this, but this also still gives me my response in a decimal format:
$result = mysql_query("select SUM(vt.vluchtdec) AS vluchttijddecimal
from tbl_vluchtgegevens vg
left join tbl_vluchttijd vt
on vg.vluchttijddec = vt.vluchttijdID
WHERE vg.vertrekdatum <=NOW();");
while($row = mysql_fetch_array($result))
{
$dec = $row['vluchttijddecimal'];
function
convertTime($dec)
{
// start by converting to seconds
$seconds = $dec * 3600;
// we're given hours, so let's get those the easy way
$hours = floor($dec);
// since we've "calculated" hours, let's remove them from the seconds variable
$seconds -= $hours * 3600;
// calculate minutes left
$minutes = floor($seconds / 60);
// remove those from seconds as well
$seconds -= $minutes * 60;
// return the time formatted HH:MM:SS
return lz($hours).":".lz($minutes).":".lz($seconds);
}
// lz = leading zero
function lz($num)
{
return (strlen($num) < 2) ? "0{$num}" : $num;
}
echo "" .$dec."";
In MS Access I would do something like this:
CInt([vluchttijddecimal]) & ":" & Format([vluchttijddecimal]*60 Mod 60;"00")
But this does not work or I don't know how to do so in MySQL / php.
For anyone that is interested... This is how you would convert decimal time (Where 0.1 == 6 minutes) to hours and minutes (0.2333 == 14 minutes) in MYSQL alone. no PHP is needed. This also accounts for the need to round seconds to minutes.
SELECT CONCAT(FLOOR(timeInDec),':', LPAD(ROUND((timeInDec - FLOOR(timeInDec)) * 60) % 60,2,0)) AS TimeInHoursMinutes
FROM YourTable;
Replace timeInDec with the column name that contains the decimal time you would like to convert.
This will return 0:06 for 0.1000 decimal value so leading zeros are accounted for in single digit minutes.
You can do this in you SQL statement something like this:
SELECT CONCAT(CEIL(mydecimal),':', LPAD(Floor(mydecimal*60 % 60),2,'0')) as formated text
Where mydecimal is your unformatted field name
I think I have calculated your time values... although it was kinda pain.
It appears your "decimal time" is "hours.minutes"? Rather horrible and definitely not a good format: for dealing with time its best to stick to integers that specify either a total of minutes/seconds/hours or whatever granularity you need.
But assuming it is hours.minutes, you should be able to do it like this in PHP:
while($row = mysql_fetch_array($result))
{
$dec = $row['vluchttijddecimal'];
return sprintf("%2d:%2d", floor($dec), floor(($dec - floor($dec))*100));
}
Hopefully I am correct in assuming that you mean, for example that 2.5 hours = 2H 30mins. If so, then your 'time' is a time interval and is best represented by the DateInterval class.
This function will do what you want:-
/**
* Converts a 'decimal time' in the format 1.5hours to DateInterval object
*
* #param Int $decimalTime
* #return DateInterval
*/
function decTimeToInterval($decimalTime)
{
$hours = floor($decimalTime);
$decimalTime -= $hours;
$minutes = floor($decimalTime * 60);
$decimalTime -= ($minutes/60);
$seconds = floor($decimalTime * 3600);
$interval = new \DateInterval("PT{$hours}H{$minutes}M{$seconds}S");
return $interval;
}
echo decTimeToInterval(512.168)->format("%H:%I:%S");
See it working
If you want to add times in the format 'H:i' without converting them to and from decimals, you can do it like this:-
function sumTimes($time1, $time2)
{
list($hours1, $minutes1) = explode(':', $time1);
list($hours2, $minutes2) = explode(':', $time2);
$totalHours = $hours1 + $hours2;
$totalMinutes = $minutes1 + $minutes2;
if($totalMinutes >= 60){
$hoursInMins = floor($totalMinutes/60);
$totalHours += $hoursInMins;
$totalMinutes -= ($hoursInMins * 60);
}
return "$totalHours:$totalMinutes";
}
echo sumTimes('12:54', '100:06') . PHP_EOL;
echo sumTimes('12:54', '100:20') . PHP_EOL;
See it working
This is what I used for my Payroll System:
SELECT If(total_late>0, LPAD(CONCAT(REPLACE(FLOOR(total_late/60) + FORMAT(total_late%60*0.01,2), '.', ':'), ':00'), 8, 0), '00:00:00') FROM MyTable
I multiplied it by 0.01 because my variables are in Seconds. Eg. 60.00 = 1min
I would suggest this to include seconds. It is based on #Richard's solutions. Just notice I've changed CEIL by FLOOR in #Richard's solution.
SET #timeInDec=1.505;
SELECT CONCAT(FLOOR(#timeInDec),':', LPAD(FLOOR(#timeInDec*60 % 60),2,'0'),':', LPAD(FLOOR(MOD(#timeInDec*60 % 60,1)*100),2,0)) as timeInDec;

Categories