i\'m attempting to make an if statement to compare two time/dates and see if the second date is less than 24 hours after the first date entered. if it is it should not allow the
This is easier than you think. Just add 24 hours to the first date and then compare it to the future date. If the future date is less than the first date, it is less than 24 hours in the future.
$plus24hours = new DateTime("+1 day");
$future_date = new DateTime($date);
if ($future_date < $plus24hours) {
// Less than 24 hours in the future
}
You can get the difference in seconds by converting the two DateTime
objects to timestamps.
$diffInSeconds = $future_date->getTimestamp() - $now->getTimestamp();
Maybe it would be better to use strtotime
, if the DateTime
objects are only required for this operation.