Fractions of 15 minutes in the PHP time log

Asked

Viewed 514 times

2

I have a billing system for hours which I am trying to make charges for fractions of 15 min. It takes the difference of the time of entry and exit and adds the remaining time, example:

  • Check-in 09:00 - Check-out 09:11
  • Real time: 00:11 - Billing time: 00:15.

And so successively every 15.

I put together a code that works for up to 60 minutes, however, my question lies in after this time passes. It does not add fractions, but normal minutes.

Is there any way to recognize that after 01:00:01 you must add the fraction of 15 successively? Below follows the code:

<?php 
$horaInicial = $_POST['inicio'];
$horaFinal = $_POST['final'];
$fator15 = 900;
$fator30 = 1800;
$fator45 = 2700;
$fator60 = 3600;
$horaMais = 3600;


$horaInicial = strtotime($horaInicial);
$horaFinal = strtotime($horaFinal);

$totalSegundos = ($horaFinal - $horaInicial);



switch($totalSegundos){
    case ($totalSegundos < $fator15):
        while($totalSegundos < $fator15){
            $totalSegundos++;
        }
        break;

    case ($totalSegundos > $fator15 && $totalSegundos < $fator30):
        while($totalSegundos < $fator30){
            $totalSegundos++;
        }
        break;

    case ($totalSegundos > $fator30 && $totalSegundos < $fator45):
        while($totalSegundos < $fator45){
            $totalSegundos++;
        }
        break;

    case ($totalSegundos > $fator45 && $totalSegundos < $fator60):
        while($totalSegundos < $fator60){
            $totalSegundos++;
        }
        break;
    case($totalSegundos > $fator60):
        while($totalSegundos < $fator15){
            $totalSegundos++;
        }

}



$resultado = gmdate("H:i", $totalSegundos);

echo "Total: ".$resultado;

?>

1 answer

2


Since you are working with the difference in minutes, you can ignore the seconds, then the first thing is to divide the difference by 60:

$totalMinutos = ($horaFinal - $horaInicial) / 60;

Then just see how many 15 minute intervals there are in this total (and using ceil to round up):

$intervalosDe15 = ceil($totalMinutos / 15);

With this you get the amount of 15 minute intervals. For example, if the initial time is 9:00 and the final time is 10:11, the result is 5.

To get the total of minutes equivalent, just multiply by 15:

$intervaloMinutos = $intervalosDe15 * 15;

And now we come to the point where you print out the result. You’re using gmdate, which prints a date in a given format. But the information you have (the interval in minutes) is not a date. There are two different concepts here:

  • one date/time represents a specific point in the timeline: a date is a point in the calendar (ex: day 2 of April 2019) and a time represents a specific point in the day (ex: two and a half in the afternoon)
  • one duration represents a quantity of time, with no direct relationship to calendars. Ex: the duration of the film is two hours (I didn’t say what time it starts, nor if in fact it will start, it’s just the amount of time, unrelated to dates or times)

Both may use the same words (days, hours, minutes, etc), but they are different concepts.

gmdate works with dates, but what you have ($intervaloMinutos) is a duration. And to work with durations, you should use a DateInterval:

$intervaloMinutos = $intervalosDe15 * 15;
$horas = floor($intervaloMinutos / 60);
$minutos = $intervaloMinutos % 60;
$interval = new DateInterval("PT{$horas}H{$minutos}M");
echo $interval->format('%H:%I'); // hh:mm

First I turn the 15 minute intervals into the total minutes. Then I take the amount of hours and minutes and create the DateInterval.

The builder of DateInterval must receive the values in the format ISO 8601. In the case, PTxHyM represents a duration of x hours and y minutes. Then I use the method format to format the DateInterval.

Using the starting time 09:00 and the final time 10:11, $intervalosDe15 shall be equal to 5, then $intervaloMinutos will be 75 (ie the total duration is 75 minutes).

Next, $horas will be 1 and minutos will be 15, and when formatting the DateInterval, the result will be 01:15.

  • 1

    Not an answer is a real lesson. I am very grateful for the help.I will implement in the code and put the result. I hope one day to reach your level!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.