Check if difference between dates is longer than X minutes in PHP

4

First of all, I've done a lot of research here on the site and found no more in-depth example, just basic questions / answers about how to get the difference between dates, what I got to do and it works the way I want. >

However, the code I currently have seems to me a bit "heavy" but since I do not have advanced knowledge in PHP, I do not know how I could simplify it.

I have a JSON file with some data, some of which needs to be updated after 30 minutes have elapsed since its creation. For this I use the following code to create the time used in the json file:

'time' => date('m/d/Y H:i:s')

When I do the date request, I then compare the date saved in the json file, with the current date. If it's longer than 30 minutes, I have to do another treatment. This is the code I currently use:

$fileTime = new DateTime($file['time']); //Data obtida do arquivo json
$currTime = new DateTime();
$interval = date_diff($fileTime, $currTime);

if($interval->y >= 1 || $interval->m >= 1 || $interval->d >= 1 || $interval->h >= 1 || $interval->i >= 30) {
    //Chama função
}

The problem with this is that if it takes 1:10 minutes and I check only the minutes, it will give a wrong result, since it's been over 30mins, so I have to check all the houses after the minute also.

Is there any way to simplify this verification?

    
asked by anonymous 05.10.2016 / 01:51

2 answers

2

I always try to give an alternative with the primitive date functions, because they are usually very simple and instead of objects returning numbers, which are of course #

Instances and methods are good when abstraction brings gain and simplicity to non-critical operations in terms of time. As in the current case there is no gain with the DateTime abstraction, we can simply do this:

$date1 = strtotime( '10/04/2016 15:00' );
$date2 = strtotime( '10/04/2016 16:10' );

If one of the dates is the current one, you can use this:

$date1 = time(); // use mktime() para hora local

To calculate the difference:

$intervalo = abs( $date2 - $date1 ) / 60;

if( $intervalo > 30 ) {
   // 
}

See the calculation on IDEONE .

Important Note that when the date is separated by traits , PHP already interprets as DD-MM-AAAA , and with forward as MM/DD/AAAA . If you need to, you can change it like this:

strtotime(str_replace('/', '-', '11/04/2016 18:10'));

The value returned is in seconds, we divide by 60 to calculate the minutes.

If you prefer, you can use this syntax if you are sure that date 2 will always be greater than 1:

$intervalo = $date2 - $date1 / 60;

And of course, you can absolutley simplify the code once you have understood the general idea:

$date1 = strtotime( '10/04/2016 15:00' );
$date2 = strtotime( '10/04/2016 16:10' );

if( $date2 - $date1 > 1800 ) {
   // 1800 segundos = 30 minutos
}

Note that if you use timestamp or datetime in the database, in many DBs like mysql, you can already get the values without strtotime until (with unix_timestamp). It saves one more step, both in data volume and data format processing.


If you need to display the value, or use current time, see this variant:

  

Convert the difference between dates to string

    
05.10.2016 / 02:35
2

Get the value getTimestamp() of the two dates and subtract one from the other, finally divide by 60 which are the seconds.

$dt1 = DateTime::createFromFormat('d/m/Y H:i', '04/10/2016 14:00');
$dt2 = DateTime::createFromFormat('d/m/Y H:i', '04/10/2016 14:31');


function diff_real_minutes(DateTime $dt1, DateTime $dt2)
{
    return abs($dt1->getTimestamp() - $dt2->getTimestamp()) / 60;
}


echo diff_real_minutes($dt2, $dt1);

Functional sample

It has examples here with the nesbot / carbon , which is a widely used date package in the world , but can be installed outside the framework that also has this calculation logic presented above.

<?php

use Carbon\Carbon;

require_once "vendor/autoload.php";

$data_old = Carbon::createFromFormat('d/m/Y H:i', '04/10/2016 15:00');
$data_now = Carbon::createFromFormat('d/m/Y H:i', '04/10/2016 15:31');

$diff = $data_now->diffInMinutes($data_old);

var_dump($diff);
    
05.10.2016 / 02:18