How to check in PHP if 2 unix timestamps are in a range on 5 minutes by using DateTime Class?
I’m refering to five minutes of clock, not (5*60 secs). When exactly the minute passes when you look at the system clock.
So it could be for example a range from 04:59 to 05:04.
In this case if the timestamps are are referred to 04:59 and 05:04 for example it should be true.
I’m having a lot of trouble with the different days and hours, otherwise a simple subtraction would have been enough.
function checker($timestamp1, $timestamp2){
$date1 = new DateTime();
$date2 = new DateTime();
$date1->setTimestamp($timestamp1);
$date2->setTimestamp($timestamp2);
$a_date = $date1->format('d m Y');
$b_date = $date2->format('d m Y');
// I'm lost exactly here
// WHAT CODE I HAVE TO WRITE FROM HERE ON?!?!
if ($a_date != $b_date) { return 0; } //WRONG CODE FOR "14 10 2024 00:00:10" AND "13 10 2024 59:00:00"
$a_hour = $date1->format('H');
$b_hour = $date2->format('H');
if (abs($a_hour - $b_hour) > 2) { return 0; }
$a = $date1->getTimestamp();
$b = $date2->getTimestamp();
if (abs($a - $b) <= 5*60 ) { return 1; } else { return 0; }
}
N.B. For what I’m doing it’s important that the function uses DateTime
Class.
You need to sign in to view this answers
Leave feedback about this