1

I'm trying to see if the 'last seen' time is within 5 minutes of the current time. If the time is, it gets added to a list. What am I doing wrong here?

Values of variables going into this:

$cell = '2020-10-30 10:39:47'

$current = '2020-10-30 10:41:39'

<?php
...
$current = date('Y-m-d H:i:s');

$datetime_from_last_seen = date("Y-m-d H:i", strtotime("-5 minutes", strtotime($cell)));
$datetime_from_current = date("Y-m-d H:i", strtotime("-5 minutes", strtotime($current)));
                     
$dtflsString = strtotime($datetime_from_last_seen);
$dtfcString = strtotime($datetime_from_current);

if ($dtflsString < $dtfcString)
{
    echo "<br>" . "Last Seen: " . $cell . " is within 5 minutes of: " . $current . "<br>";
    array_push($my_array_of_times_seen, $cell);
}
...
?>
YLR
  • 1,503
  • 4
  • 21
  • 28
drowsy99
  • 33
  • 3
  • what are the values for $current and $cell? – Angel Deykov Oct 30 '20 at 14:02
  • 2
    None of this code really makes any sense in terms of what you're trying to achieve, to be honest. But anyway this task is well understood and there are plenty of solutions already. Don't re-invent the wheel, do some simple googling instead. Here's a canonical SO answer to this problem: [How to get time difference in minutes in PHP](https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – ADyson Oct 30 '20 at 14:02

1 Answers1

2

You have way too many unnecessary conversions going on. I think time() is all you need here.

Using your $cell value date format:

<?php

$current = time();
$last_seen = strtotime($cell);

$difference = $current - $last_seen;

if ($difference >= 0 && $difference <= (60*5)){ //within and including exactly 5 minutes
    array_push($my_array_of_times_seen, $cell);
}

?> 
Onimusha
  • 3,348
  • 2
  • 26
  • 32