Reputation: 681
i have a Mysql table shift_def in the following format.
+-------------------------------------------------------------------------------+
| id | name | start_time | end_time | description | break |
|-------------------------------------------------------------------------------|
| 101 | Shift1 | 01:03:55 | 06:00:55 | Shift 1 | 1 |
|-------------------------------------------------------------------------------|
| 102 | Shift2 | 06:03:55 | 01:00:55 | Shift 2 | 3 |
+-------------------------------------------------------------------------------+
to find the total shift duration. i perform the following process in my php.
$shift_time = mysql_query("select start_time, end_time from rpt_shift_def where name ='Shif1'")or die(mysql_error());
while ($row = mysql_fetch_assoc($shift_time))
{
$Total_shift_time = strtotime($row['end_time']) - strtotime($row['start_time']);
$hours=floor($Total_shift_time/3600);
$Total_shift_time-=$hours*3600;
$minutes=floor($Total_shift_time/60);
$Total_shift_time-=$minutes*60;
$seconds=$Total_shift_time;
$Total_shift_time=$hours.":".$minutes.":".$seconds;*/
echo "$Total_shift_time";
echo "</br>";
}
the output is
4:57:0
when i try the same for shift 2 the expected output is
18:57:0
but the output is
-6:57:0
the negative value is because end_time of the shift 2 is less than start_time. how to solve this problem.
can anyone help me out.
Upvotes: 2
Views: 791
Reputation: 63471
If end time is less than start time, add 24 hours (the reason being that the shift ended on the following day).
Since you have already calculated $Total_shift_time
, you can just check if that is negative.
if( $Total_shift_time < 0 ) {
$Total_shift_time += 86400; // Number of seconds in a day
}
Err... Make sure you do this after you first calculate the shift time and before you start calculating hours, minutes and seconds...
Upvotes: 1