Error when calculating time difference with Datetime after 00:00

Asked

Viewed 608 times

1

I’m having a problem figuring out the time between two times with the datetime when the departure time is between midnight and 1 a.m..

For example, if the first time is 14:00, and the second time is 23:00, it returns right:

9 o'clock

But if the second time is 00:00, it returns:

14 hours

When I should return 10 hours.

If I put the second time at 01:00, it still returns wrong:

13 o'clock

But if I put 2:00, then it already returns right again:

12 o'clock

THE HTML:

 <label for="Cseg5">Entrada:</label>
 <input type="time" id="Cseg5" name="Tsegsss">
 <label for="Cseg6">Saída:</label>
 <input type="time" id="Cseg6" name="Tsegssss"> 

PHP:

$val1 = $_POST ["hora1"];
$val2 = $_POST ["hora2"];

$datetime1 = new DateTime($val1);
$datetime2 = new DateTime($val2);

$intervalo = $datetime1->diff($datetime2);

To turn the property into a variable:

$horario1 = $intervalo->h;

Follows the var_dump from $val1, $val2, $horario1 and $interval, with fields filled with 14:00 and 00:00.

string(5) "14:00"

string(5) "00:00"

int(14)

Object(Dateinterval)#30 (15) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(14) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(1) ["days"]=> int(0) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }

From what I understand it is subtracting backwards when it passes of the midnight (until the 01 hour). I have tried to invert the variables...

 $intervalo = $datetime2->diff($datetime1);

But it didn’t. Any ideas?

  • What version of php are you using? you can see this by creating a file with the following content <?php phpinfo();

  • PHP Version 5.5.9-1ubuntu4.7

  • I did some tests here I got the expected result 10 horas, what I did differently was to spend a full date in place of only one hour. Test - example

  • So, but I want to not have to pass the date. There are several fields <input type="time"> just with HH:MM, and I need to calculate the difference of hours and minutes between them... Is there no way with datetime? Isn’t there some way to "fool" this? Make sure that every time it’s past 11:59 pm, it’s like another day? Thanks for the time being!

  • To illustrate better I edited the question and put the HTML. It’s a work schedule, which can extend until after 00:00, so it makes no difference the date, only the time. It seems that it only works properly if you inform the date... You can inform any date, just for him to count the time right, since I am sending the time by form?

  • So if you don’t report the date how will he know that $val1 is less than $val2 would return a negative value.

  • So I wanted to "force" this, to report somehow that val2 is always greater than $val1...

  • ehehuehu I got(MOS)! $datetime1 = new Datetime("2015-04-05, $val1"); and $datetime2 = new Datetime("2015-05-06, $val2"); It’s kind of a gambit right? : ) Let me try again haha

  • That’s right, and it was you who gave me the letter, thanks! Again! And it was before 1:00!

Show 4 more comments

1 answer

1


Problem

To calculate the interval of hours in the expected way it is necessary to inform the date and time so the datetime object knows whether they are of the same day or of different days.

In your example if you run a print_r in $datetime1 and $datetime2 only with the schedule will be printed something like:

$datetime1:

DateTime Object
(
    [date] => 2015-04-18 14:00:00
    [timezone_type] => 3
    [timezone] => America/New_York
)

$datetime2

DateTime Object
(
    [date] => 2015-04-18 00:00:00
    [timezone_type] => 3
    [timezone] => America/New_York
)

Solution

When calculating date/time difference returns negative invert of the object DateInterval returns 1, in this case just add one day in $datetime2 and recalculate the difference.

function calcularIntervaloHoras($horaInicio, $horaFim){
    $intervalo = $horaInicio->diff($horaFim);

    if($intervalo->invert == 1){
       $horaFim->add(new DateInterval('P1D'));
       $intervalo = $horaInicio->diff($horaFim);   
    }

    return $intervalo;

}

phpfiddle - example

  • Very good! Thanks again!

Browser other questions tagged

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