Checking the difference between two dates

后端 未结 2 1270
执笔经年
执笔经年 2021-01-26 07:37

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

相关标签:
2条回答
  • 2021-01-26 08:19

    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
    }
    
    0 讨论(0)
  • 2021-01-26 08:25

    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.

    0 讨论(0)
提交回复
热议问题