Calculate Interval of Hours

Asked

Viewed 1,235 times

2

I need to calculate the minutes between two dates. My problem is to calculate when a day goes by.

Example:

Primeira Data -> 2016-11-18 15:20:00;
Segunda Data -> 2016-11-18 15:45:02;
  • You need to know the difference in minutes between dates ?

  • Yeah, that’s right!

  • There’s already some code made ?

  • I have, $horario1 = "13:10:50"; $horario2 = "13:40:00"; echo "Difference between times: " . dif_time($horario1, $horario2). " <br/>"; // Function to calculate time Function dif_time($horario1, $horario2) { $horario1 = strtotime("1/1/1980 $horario1"); $horario2 = strime totime("1/1/1980 $horario2"); if ($horario2 < $horario1) { $horario2 = $horario2 + 86400; } $value = (($horario2 - $horario1) / 3600) * 60; Return Ceil($value); }

3 answers

4


A good solution to check this is to use strtotime(); (of course) and abs();

$t1 = strtotime("18-11-2016 15:12:00");
$t2 = strtotime("18-11-2016 15:30:00");
echo round(abs($t1 - $t2) / 60,2). " minutos ";

In addition to Erlon’s solution, you can use this as well.

  • 1

    Please understand how a constructive criticism, when you notice a question that probably already has answers on the site, favors the answers existing in the old questions, thus preventing the content to be spread. For this just vote or signal as duplicate, worth to point the link in the comments.

  • Thank you, William. In case, when it was marked as duplicate, I had already posted. The right would be to delete ?

  • That’s not what I meant, the action I recommended was before duplicating, in case you can even research if the question has not already been answered, even more a simple question so you will probably already have answer. The question is to favor existing responses

  • 1

    Note: No need to delete this, the tip is for future posts.

2

$datatime1 = strtotime('2016-11-18 15:20:00');
$datatime2 = strtotime('2016-11-18 15:45:02');
$segundos = ($datatime2 - $datatime1);
$inteiro = (int)($segundos / 60);
$resto = ($segundos-($inteiro*60));
echo "A diferença em minutos é { ".$inteiro." } minutos e " .$resto. " segundos";
  • 1

    Please understand how a constructive criticism, when you notice a question that probably already has answers on the site, favors the answers existing in the old questions, thus preventing the content to be spread. For this just vote or signal as duplicate, worth to point the link in the comments.

2

You can use the date_diff()

<?php
$datetime1 = date_create('2016-11-18 15:20:00');
$datetime2 = date_create('2016-11-18 15:45:02');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

The code above will return

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 25
    [s] => 2
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 0
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
  • 1

    Please understand how a constructive criticism, when you notice a question that probably already has answers on the site, favors the answers existing in the old questions, thus preventing the content to be spread. For this just vote or signal as duplicate, worth to point the link in the comments.

Browser other questions tagged

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