Time Duration between 2 DateTimes in PHP is not accurate when over a day
has past
I am working on a Timeclock application for my boss and I store a clock in
and clock out time as a DateTime and as a Timestamp.
Below is a simple function to calculate the duration between a user's
Clock in and Clock out DateTimes.
In the demo you can see the result is 15:11:18 even though there is like 5
days between the 2 dates. I know I could simply make the function show the
number of days but my boss does not want to show days, instead this should
should the Total Hour, minutes, seconds between the 2 DateTimes and not
show the Days.
Can anyone point me in the right direction or help to make it show the
proper amount of time?
public function dateTimeDifference($startDatetime, $endDatetime, $format =
'%H:%I:%S')
{
$startDatetime2 = new DateTime($startDatetime, $this->timeZone);
$endDatetime2 = new DateTime($endDatetime, $this->timeZone);
$interval = $startDatetime2->diff($endDatetime2);
return $interval->format($format);
}
$startDatetime = '2013-09-10 07:15:48 pm';
$endDatetime = '2013-09-15 10:27:06 am';
echo dateTimeDifference($startDatetime, $endDatetime)
// Result: 15:11:18
No comments:
Post a Comment