Calculate Time Range [duplicate]

2

I need to calculate the minute interval between two dates. My problem is being calculating when a day goes by.

Example:

Primeira Data -> 2016-11-18 15:20:00;
Segunda Data -> 2016-11-18 15:45:02;
    
asked by anonymous 18.11.2016 / 19:23

3 answers

4

A good solution to check this is to use strtotime(); (clear) 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 the Erlon solution, you can use this too.

    
18.11.2016 / 19:32
2

You can use 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
)
    
18.11.2016 / 19:31
1
$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";
    
18.11.2016 / 20:16