Check if date is in the future in PHP

Check if date is in the future in PHP

Posted by Luke Beeno on July 27, 2022

Check if date is in the future in PHP

Check if date is in the future in PHP

Posted by Luke Beeno on July 27, 2022

To check if a date is in the future using PHP8:

Step 1. Get current date using date()

Step 2. Convert date to seconds

Step 3. Convert the date to be checked to seconds

Step 4. Check if the date is greater than the current date

Current date:

$date = date('Y-m-d')

Date to seconds:

$current = strtotime($date)

Date to be checked in seconds:

$d = strtotime('2022-07-26')

Check date greater than current date

return $d > $current

Full example below:

function isDateInFuture($day){
    $currentDay = date('Y-m-d');
    $dateInSeconds = strtotime($currentDay);
    
    return $day > $dateInSeconds;
}

$d = strtotime('2022-07-26');
$d1 = strtotime('2022-08-26');

var_dump(isDateInFuture($d)); //bool(false);
var_dump(isDateInFuture($d1)); //bool(true);