Question

I have a problem. I have a certain date/time (A), which is put in a variable. Lets say its May 06 2014 14:26. I want to compare this date/time with file's modification date/time (B), and if it is the same, I put the file's modification date/time (B) in variable too. The thing is, I cant get exactly the same date on file (B). So, I want that it would add 5 minutes difference. For example, if file (B) has modification date May 06 2014 14:28, it will be put in a variable. Or if it has modification date May 06 2014 14:22, it will be put in a variable too. If it helps, I am using date("F d Y H:i",$file['filetime']) command to get the modification date of the file. Any possibility to do this? Thanks in advance.

If something is unclear, just tell me. I'm new here, so might be hard to explain things correctly.

Was it helpful?

Solution 2

With the DateTime class, it is done in a few lines :

$datetime1 = new DateTime('2009-10-13 10:00:00');
$datetime2 = new DateTime('2009-10-13 10:20:00');
$interval = $datetime1->diff($datetime2);
$yourIntervalle = $interval->format('%i minutes'));

OTHER TIPS

Maybe, it helps:

$datetimeA = strtotime($file1['filetime']);
$datetimeB = strtotime($file2['filetime']);

$interval  = abs($datetimeA - $datetimeB);
$minutes   = round($interval / 60);

if ($minutes > 5) {
 //do some magic here  
}

If $file['filetime'] is a timestamp already, then You can use it without calling strtotime().

i'd either directly use unix-timestamps or convert A and B to Unix Timestamps (e.g. using strtotime) and compare them based on your requirements. if you have access to the file directly you could even use filemtime to directly get the last modified time as an unix timestamp.

Guess this is what you're looking for, if your problem is just to add 5 minutes:

$dateB = new DateTime('2014-05-06 14:26:00');
$interval = new DateInterval('PT5M');

$dateB->add($interval);

print_r($dateB);

PHPSandbox: http://sandbox.onlinephpfunctions.com/code/9f9a2ee40f09f953c796c9c2dd7e15ad62b45772

Also, look at DateInterval to understand a little more: http://www.php.net/manual/en/dateinterval.construct.php

you can do like. if you want to minus 5 minutes from the date

$given_date = 'May 06 2014 14:28';    
$date = new DateTime($given_date);
//subtract 5 minutes from the date
$new_date = $date->sub(new DateInterval('PT' . '5' . 'M'));        
echo $new_date->format('F d Y H:i'); 

if you want to add 5 minutes then change

//add 5 minutes to the date
$new_date = $date->add(new DateInterval('PT' . '5' . 'M'));

First of all, i think you need to compare the two date/time by the method strtotime. With that you can compare the two date. Then, you just need to make one or two insert in your database if i understand correctly with a If/Else method.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top