ecabrera Posted April 10, 2015 Share Posted April 10, 2015 Im trying to add seconds like this $h = str_replace("h ",":","$totalTime"); $m = str_replace("m ",":","$h"); $all = str_replace("s","","$m"); $parsed = date_parse($all); $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second']; echo $seconds; the results come out like this 12766 0 4460 0 9219 0 0 which i think is good because there are seven results 12766 , 0 , 4460 , 0 , 9219 , 0 , 0 i want to add them but keep in mind that they are in sign of an array. I've been trying this $seconds * 7; or $seconds + seconds + seconds + seconds + seconds + seconds +seconds but i get this 89362 0 31220 0 64533 0 0 is there a way to add them all up and get just on value of seconds? like 3294242 Quote Link to comment https://forums.phpfreaks.com/topic/295411-adding-seconds/ Share on other sites More sharing options...
Barand Posted April 10, 2015 Share Posted April 10, 2015 Here's an example for you The data mysql> SELECT * FROM timetest; +----+----------+---------------------+---------------------+ | id | staff_id | start_time | end_time | +----+----------+---------------------+---------------------+ | 1 | 1 | 2015-04-06 08:35:00 | 2015-04-06 17:35:00 | | 2 | 1 | 2015-04-07 08:30:00 | 2015-04-07 17:50:00 | | 3 | 1 | 2015-04-08 09:35:00 | 2015-04-08 17:55:00 | | 4 | 1 | 2015-04-09 08:30:00 | 2015-04-09 17:20:00 | | 5 | 1 | 2015-04-10 08:20:00 | 2015-04-10 17:50:00 | | 6 | 2 | 2015-04-06 08:15:00 | 2015-04-06 18:10:00 | | 7 | 2 | 2015-04-07 08:25:00 | 2015-04-07 18:50:00 | | 8 | 2 | 2015-04-08 08:30:00 | 2015-04-08 17:20:00 | | 9 | 2 | 2015-04-09 08:10:00 | 2015-04-09 17:00:00 | | 10 | 2 | 2015-04-10 08:00:00 | 2015-04-10 17:00:00 | +----+----------+---------------------+---------------------+ The query to total the times SELECT staff_id , DATE(start_time) , SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, start_time, end_time))) as tot FROM timetest GROUP BY staff_id, DATE(start_time) WITH ROLLUP; The results +----------+------------------+----------+ | staff_id | DATE(start_time) | tot | +----------+------------------+----------+ | 1 | 2015-04-06 | 09:00:00 | | 1 | 2015-04-07 | 09:20:00 | | 1 | 2015-04-08 | 08:20:00 | | 1 | 2015-04-09 | 08:50:00 | | 1 | 2015-04-10 | 09:30:00 | | 1 | NULL | 45:00:00 | <- TOTAL FOR staff 1 | 2 | 2015-04-06 | 09:55:00 | | 2 | 2015-04-07 | 10:25:00 | | 2 | 2015-04-08 | 08:50:00 | | 2 | 2015-04-09 | 08:50:00 | | 2 | 2015-04-10 | 09:00:00 | | 2 | NULL | 47:00:00 | <- TOTAL FOR staff 2 | NULL | NULL | 92:00:00 | <- GRAND TOTAL +----------+------------------+----------+ Quote Link to comment https://forums.phpfreaks.com/topic/295411-adding-seconds/#findComment-1508698 Share on other sites More sharing options...
ecabrera Posted April 10, 2015 Author Share Posted April 10, 2015 (edited) This is how my code looks like. $query = "SELECT date, start, end, day_total, TIMESTAMPDIFF(SECOND, start, end) as day_t FROM `timesheet` WHERE `date` >= CAST('$start' AS DATE) AND `date` <= DATE_ADD(CAST('$start' AS DATE), INTERVAL 6 DAY) AND e_user='$username'"; $result = mysqli_query($db, $query); //Dump results into an array $records = array(); while($row = mysqli_fetch_assoc($result)) { $records[$row['date']] = $row; } //Create a loop from first to last date //for each day, check if there is a matching records //in the records array. If so, use that data //Else, there was no data for that date for($day=0; $day<7; $day++) { $timeStamp = strtotime("{$start} +{$day} day"); $dateStamp = date("Y-m-d", $timeStamp); $DOW = date("D", $timeStamp); $dateStr = date("m/d/Y", $timeStamp); //Set default values $startTime = ''; $endTime = ''; $totalHours = ''; if(isset($records[$dateStamp])) { $startTime = $records[$dateStamp]['start']; $endTime = $records[$dateStamp]['end']; $totalTimeer = $records[$dateStamp]['day_t']; } // $totalTime = $records[$dateStamp]['day_total']; $totalTime = $records[$dateStamp]['day_total']; $h = str_replace("h ",":","$totalTime"); $m = str_replace("m ",":","$h"); $all = str_replace("s","","$m"); $parsed = date_parse($all); $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second']; echo $seconds; //Output results echo "<tr>\n"; echo "<td class='tg-031e'>{$DOW} {$dateStr}</td>\n"; echo "<td class='tg-031e'>{$startTime}</td>\n"; echo "<td class='tg-031e'>{$endTime}</td>\n"; echo "<td class='tg-031e'>{$totalTime}</td>\n"; echo "<td class='tg-031e'></td>\n"; echo "<td class='tg-031e'></td>"; echo "</tr>\n"; } ?> Edited April 10, 2015 by ecabrera Quote Link to comment https://forums.phpfreaks.com/topic/295411-adding-seconds/#findComment-1508699 Share on other sites More sharing options...
Solution ecabrera Posted April 10, 2015 Author Solution Share Posted April 10, 2015 i fixed it by adding $seconds = 0; before the for loop $seconds = 0; for($day=0; $day<7; $day++) { ... $seconds += $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second']; } echo $seconds; Quote Link to comment https://forums.phpfreaks.com/topic/295411-adding-seconds/#findComment-1508701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.