I have a table that has start and end datetime stamps and time calculation per record, like this:
start time end time time it took
2015-02-11 16:32:57 2015-02-11 17:23:34 0 day 00 hr. 50 min.
2015-02-12 11:59:03 2015-02-12 12:05:53 0 day 00 hr. 6 min.
2015-02-12 12:33:52 2015-02-12 12:43:22 0 day 00 hr. 9 min.
2015-02-12 12:43:12 2015-02-13 09:56:08 0 day 21 hr. 12 min.
2015-02-13 09:36:49 2015-02-13 09:53:55 0 day 00 hr. 17 min.
How can I get the earliest datetime and latest datetime, so i can do a calculation on those 2 datetimes to get a total time for all records combined?
I assume the earliest datetime entry will be the start time of first record, and the latest datetime entry will be the end time of last record.
Update
I asked the question wrong. I can't select first start datetime and last end date time, because that counts all the time in between the start-end pairs. I need to add up all the "time it took" entries. How is that done?
If it's easier to store all the times in seconds then convert on the page, let me know, then provide solution for that method. Thanks.
Another Update
So after changing all the time data in the database to seconds, i used the following to get what i need. Sometimes (actually many times) when people give answers they give little snippets of code and don't elaborate or give examples on how to use said snippet. For instance, SUM(time) by itself does nothing and I didn't know how to implement it. I needed to go out and search how to use it and the following is what I found. on a side note, not sure what good a question and answer site is, if i have to go look for extended (more elaborate) code somewhere else.
$result = mysql_query("SELECT SUM(time) AS time_sum FROM table WHERE jobnum='".$jobnum."'");
$row = mysql_fetch_assoc($result);
$sumoftime = $row['time_sum'];
//echo $sumoftime; //as seconds
echo secondsToWords($sumoftime); //as words with days, hours, minutes, and seconds from a separate function
The separate function:
function secondsToWords($seconds) {
$ret = "";
/*** get the days ***/
$days = intval(intval($seconds) / (3600*24));
if($days> 0) {
$ret .= "$days days ";
}
/*** get the hours ***/
$hours = (intval($seconds) / 3600) % 24;
if($hours > 0) {
$ret .= "$hours hours ";
}
/*** get the minutes ***/
$minutes = (intval($seconds) / 60) % 60;
if($minutes > 0) {
$ret .= "$minutes minutes ";
}
/*** get the seconds ***/
$seconds = intval($seconds) % 60;
if ($seconds > 0) {
$ret .= "$seconds seconds";
}
return $ret;
}
I would do the following:
$stmt = "select
start_time,
end_time
from times
";
// Add all results to an array
// Loop through the array of start and end times
foreach($times as $time) {
$total += (new \DateTime($time['end_time']))->getTimestamp() - (new \DateTime($time['start_time']))->getTimestamp();
}
// Get Hours, minutes and seconds
$hours = floor($total/3600);
$total -= $hours * 3600;
$minutes = floor($total/60);
$total -= $minutes * 60;
$seconds = floor($total);
// Output
echo "{$hours}h {$minutes}m {$seconds}s";