Sum with dates and strtotime in PHP

Asked

Viewed 2,021 times

4

In this small script of a service system, I save the beginning of the service (field DATETIME bank) and the estimated term of its end (field TIME at the bank).

Look at:

$d1 = "2019-09-23 12:30:00"; // Data e hora que o atendimento começou
$d2 = "01:00:00"; // Tempo esperado para finalizar o atendimento

$teste = strtotime($d1) + strtotime($d2);
echo date("d/m/Y H:i:s", $teste); // Imprime -> 25/05/2069 10:30:00

The expected result should be 23/09/2019 13:30:00, but gives the printed result above.

Why?

  • 1

    I think this php function might help you: https://www.php.net/manual/en/datetime.add.php

  • 1

    Possible duplicate of Add days to a date

2 answers

4


You are confusing dates and times with durations. Consider these two sentences:

  • the movie starts at two o'clock in the afternoon
  • the film lasted two hours

In the first, "two hours" refers to a time: a specific time of the day. In the second, "two hours" refers to the duration: the amount of time, unrelated to a specific time (it does not say what hours began or ended, it only says how long lasts).

What may be confusing is that they both use the same words ("hours", "minutes", etc.), but they are different concepts. A date represents a specific point in a calendar (day, month and year), and a time represents a specific time of a day. Already a duration is only a amount of time, and is not associated with a specific instant.


That said, strtotime('01:00:00') creates a date (not a duration) for today, with the time set at 1 am:

echo date('Y-m-d H:i:s', strtotime('01:00:00')); // 2019-09-03 01:00:00

In fact strtotime returns the timestamp corresponding to the date above, using the default Timezone that is configured in PHP.

If you want to add hours to a date, you can use the special formats which are accepted by strtotime, which allows adding lengths to a date/time, for example + X hours to add X hours. In this case, your string that corresponds to the length has hour, minute and second, then it would look something like this:

$data = '2019-09-23 12:30:00';
$duracao = '01:00:00';
$v = explode(':', $duracao);
echo date('d/m/Y H:i:s', strtotime("{$data} + {$v[0]} hours {$v[1]} minutes {$v[2]} seconds"));

I use explode to break the string in hours, minutes and seconds (the values are in the array $v). Then I mount the corresponding string, containing the date and using the syntax + duracao to add the duration to the date.

The exit is:

23/09/2019 13:30:00
  • 1

    Your answer was ideal, because you answered what I asked: "Why". Thank you!

0

The right thing would be:

<?php

$d1 = "2019-09-23 12:30:00"; // Data e hora que o atendimento começou
$d2 = "+ 1 hours"; // Tempo esperado para finalizar o atendimento

$teste = strtotime($d1 . $d2);

echo date('d/m/Y H:i:s', $teste);

where you inform the amount of hours + 1 hours on the date of commencement of service.

ONLINE EXAMPLE

  • There is always someone wanting to belittle the work of us, here it is always ... always ...

Browser other questions tagged

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