I’m developing a simple sistem so our employees could save their overtime so, at the end of the month, they’re payed for those extra hours they made.
Normal hours are counted as 1 but nightly ones (through 23:00 to 07:00) should be 1,25 hours each.
That said, we’re requesting to introduce the day, and the start and end hours. So, I thought I could have an array like this:
$hours= array(
'0' => true, '1' => true, '2' => true, '3' => true, '4' => true, '5' => true, '6' => true,
'7' => false, '8' => false, '9' => false, '10' => false, '11' => false, '12' => false, '13' => false,
'14' => false, '15' => false, '16' => false, '17' => false, '18' => false, '19' => false, '20' => false,
'21' => false, '22' => false, '23' => true
);
So, basically I test if an hour is special or it isn’t with a loop like this:
$normals = 0;
$specials = 0;
$hour_since = '23:00:00';
$hour_since_expl = explode(':',$hour_since);
$hour_to = '23:15:00';
$hour_to_expl= explode(':',$hour_to );
$date = $fecha = '2012-03-14';
$datetime_since= strtotime($date ." ".$hour_since );
$datetime_to= ((int) $hour_to_expl[0] > (int) $hour_to_expl[0]) ?
strtotime(date("Y-m-d h:i:s", strtotime($date ." ".$hour_to)) . " +1 day") :
strtotime($date." ".$hour_to);
$difference = $datetime_to - $datetime_since;
$hours_difference = $difference / SECONDS_PER_HOUR; //60*60
for ($i = 0; $i <= $difference; $i++){
$hour = $i + (int) $hour_since_expl [0];
if ($hours[$hour]) //Special hour here... Pay more!
$specials++;
else
$normals++;
}
But the problem is when hours are not exact and you have started somewhere like 22:30 and ended 00:30 where you have 0,5 hours being not special. I’ve been struggling my mind but I can’t find any solution.
Do someone have any ideas?
Edit: More code given.
Let’s assume that start time is 15:15 and endtime is 22:45. First, convert start time to nearest HOUR on or after it and endtime to nearest HOUR on or before it. So we get 15:00 and 22:00. Use your loop for these hours and use the differences as follows: if the difference time (15:15-15:00 or 22:45-22:00) is in overtime, then do overtime*numMins/60 else do standard*numMins/60. I’m not familiar with PHP, so had to do this in pseudo code