Методы проверки того, находится ли дата в прошлом, с помощью PHP

Чтобы проверить, прошла ли дата с помощью PHP, вы можете использовать несколько методов. Вот несколько примеров:

Метод 1. Использование функций date() и strtotime()

$givenDate = '2022-12-31'; // The date to check
$currentDate = date('Y-m-d'); // Get the current date
if (strtotime($givenDate) < strtotime($currentDate)) {
    echo 'The given date is in the past.';
} else {
    echo 'The given date is in the future.';
}

Метод 2. Использование класса DateTime

$givenDate = '2022-12-31'; // The date to check
$currentDate = new DateTime(); // Create a DateTime object for the current date
$givenDateObj = new DateTime($givenDate); // Create a DateTime object for the given date
if ($givenDateObj < $currentDate) {
    echo 'The given date is in the past.';
} else {
    echo 'The given date is in the future.';
}

Метод 3. Использование функции strtotime() и операторов сравнения

$givenDate = '2022-12-31'; // The date to check
if (strtotime($givenDate) < time()) {
    echo 'The given date is in the past.';
} else {
    echo 'The given date is in the future.';
}

Метод 4. Использование класса DateTime и операторов сравнения

$givenDate = '2022-12-31'; // The date to check
$currentDate = new DateTime(); // Create a DateTime object for the current date
$givenDateObj = new DateTime($givenDate); // Create a DateTime object for the given date
if ($givenDateObj < $currentDate) {
    echo 'The given date is in the past.';
} else {
    echo 'The given date is in the future.';
}