Conditional with dates is not analyzed by the Else operator?

Asked

Viewed 45 times

-3

I have the following situation, a method that calculates the difference between the beats of a point and the sum of intervals that the collaborator performs during his working day:

    //calcula as diferenças das batidas do ponto diário do colaborador
    $d1 = gmdate('H:i', strtotime('2021-08-06 15:00') - strtotime('2021-08-06 12:00')); 
    $d2 = gmdate('H:i', strtotime('2021-08-06 15:30') - strtotime('2021-08-06 15:00')); 
    $d3 = gmdate('H:i', strtotime('2021-08-06 17:00') - strtotime('2021-08-06 15:30')); 
    $d4 = gmdate('H:i', strtotime('2021-08-06 17:30') - strtotime('2021-08-06 17:00')); 
    $d5 = gmdate('H:i', strtotime('2021-08-06 21:00') - strtotime('2021-08-06 17:30')); 
    
    //subtotal: soma dos horários trabalhados

    $minutos = date("i", strtotime($d3));
    $hora = date("H", strtotime($d3));

    $subtotal = strtotime("+$minutos minutes", strtotime($d1));
    $subtotal = strtotime("+$hora hours", $subtotal);


    $minutos = date("i", strtotime($d5));
    $hora = date("H", strtotime($d5));

    $subtotal = strtotime("+$minutos minutes", $subtotal);
    $subtotal = strtotime("+$hora hours", $subtotal);

    //intervalo: soma de dos intervalos realilizados

    $minutos = date("i", strtotime($d4));
    $hora = date("H", strtotime($d4));

    $interval = strtotime("+$minutos minutes", strtotime($d2));
    $interval = strtotime("+$hora hours", $interval);

    //calculo p/ horas a mais ou devendo
    if (strtotime($subtotal) >= strtotime("08:00")) {
        $extra = strtotime("-8 hours", $subtotal);
    } else {
        //N CAI AQUI!!!
    }

    $data = [
        'd1' => $d1,
        'd2' => $d2,
        'd3' => $d3,
        'd4' => $d4,
        'd5' => $d5,        
        'subtotal' => "Subtotal: " . date("H:i", $subtotal),
        'intervalo' => "Intervalo: " .  date("H:i", $interval),
        'extra' => "Extra: " . date("H:i", $extra)
    ];

    echo '<pre>';
    print_r($data);
    echo  '</pre>';

It happens that on my parole, if the collaborator has a subtotal of 07:50, this is not analyzed by the operator else.
I don’t know what’s wrong.
I also accept recommendations for cleaner code.

  • 1

    What is the value of $subtotal when you’re not in the Else?

  • In the code I posted the subtotal is 08:00 so far okay, but you need to put a contributor input in arrears, as for example his arrival at the company at 12:10, so the subtotal will be 07:50 there that does not enter into Else!

  • 1

    Please see how to make a [mcve], it is no use to open a question pointing out an error and give an example that works as expected. It’s like going to the doctor when you’re healthy and saying, "I’m fine now, but make the appointment so when I’m sick I already know what I have".

  • 1

    My guess is it should be if ($subtotal >= strtotime("08:00")), for $subtotal is already the return of strtotime, then it doesn’t make sense to pass it back to the same function (I did some tests here and saw that PHP converts its value to string and does very strange things, debug and see the value returned by strtotime($subtotal)). Anyway, PHP doesn’t have a decent native way of working with durations (or dates, if you ask me :-D), but the way it’s done, I think it’s getting too complicated...

  • 1

    Dates and durations are different things (a date/time is a specific moment, a duration is a amount of time): strtotime and gmdate work well with dates, but not with durations (if passing 24 hours gives problem, for smaller values "works" by coincidence). Take a look at here, here, here and here to better understand and have some alternatives on how to make these calculations.

  • Thanks @Woss for the collaboration, but it was in the rush, I want to adapt better to my published questions!

Show 1 more comment

1 answer

0


Does not fall into Else because the subtotal value is 8h, so it is equal to or greater than 8h.

    Array
(
    [d1] => 03:00
    [d2] => 00:30
    [d3] => 01:30
    [d4] => 00:30
    [d5] => 03:30
    [subtotal] => Subtotal: 08:00
    [intervalo] => Intervalo: 01:00
    [extra] => Extra: 00:00
)

Just exchange the result of

$minutos = date("i", strtotime($d5));
$hora = date("H", strtotime($d5));

for

$minutos = date("i", strtotime($d5));
$hora = date("H", strtotime($d4));

that the subtotal will decrease and will fall in Else

  • In fact the dynamic is that when putting the input for example in arrears on $D1: a knock at 12:10 means that the collaborator made a total of 7:50, so would have to enter in the Else, see there forehead so to see!

Browser other questions tagged

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