I'm building a check in and out system for my work, the employee can set a start time and after his shift he can set the end time. but here is the problem:
If i start at 17:00 till 23:15 I want to show te result as 6.25 hours worked.
For example 15 minutes (¼ hour) equals .25, 30 minutes (½ hour) equals .50, etc.
Googled a lot of this but i can't find a solution for php. Someone who can help me with this? :)
Many thanks.
There isn't one function, but the DateTimeInterface
(http://php.net/manual/en/class.datetimeinterface.php) provides some tools to help, along with some basic maths:
<?php
const secsInAnHour = 3600;
const minsInAnHour = 60;
const hoursInADay = 24;
const decimalPlaces = 2;
$date1 = new DateTime();
$date2 = new DateTime();
$date1->setTime(17, 00);
$date2->setTime(23, 15);
$diff = $date2->diff($date1);
$convertToHours = $diff->s / secsInAnHour + $diff->i / minsInAnHour + $diff->h + $diff->days * hoursInADay;
$hours = round($convertToHours, decimalPlaces);
// Hours between two dateTimes, to 2 dp.
echo $hours;
You could put this into a function inside your implementation, if you wanted to (with start and end times as args).