Antonio Laguna
Antonio Laguna

Reputation: 9292

How to differentiate between hours?

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.

Upvotes: 0

Views: 167

Answers (3)

Chetter Hummin
Chetter Hummin

Reputation: 6817

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

Upvotes: 1

MakuraYami
MakuraYami

Reputation: 3428

edit: took a bit of time, but i love intresting chalanges ;D i hope it helps!

$specialStart = 23;
$specialEnd = 7;

$normals = 0;
$specials = 0;

$hour_since = '22:00:00';
$hour_since_expl = explode(':',$hour_since);
$start_time = mktime($hour_since_expl[0], $hour_since_expl[1], $hour_since_expl[2]);

$hour_to = '07:30:00';
$hour_to_expl= explode(':',$hour_to );
$end_time = mktime($hour_to_expl[0], $hour_to_expl[1], $hour_to_expl[2]);

if($end_time < $start_time){
    //worked passed minight, add a day
    $end_time = $end_time + 86400;  
}

$work_time = ( $end_time - $start_time ) / 60;

for($i = 0; $i < $work_time; $i++){
    $time = $start_time + ( $i * 60 );
    $hour_of_day = date("H", $time);
    if($hour_of_day >= $specialStart || $hour_of_day < $specialEnd){
        $specials += 60;    
    }else{
        $normals += 60;
    }
}

$specials = $specials / 3600;
$normals = $normals / 3600;
echo "specials: ".$specials;
echo "<br/>normals: ".$normals;

Upvotes: 1

heyanshukla
heyanshukla

Reputation: 669

I would suggest you to calculate total time in two separate fields.Firstly get normal hours i.e. before 23:00 and then special hours i.e. after 23:00 and before 07:00.

Then you can calculate pay easily.

Upvotes: 0

Related Questions