PASSWORD RESET

Your destination for complete Tech news

PHP

How to check if a date is between two dates in PHP?

3.49K 0
< 1 min read

To check if a date is between two dates in PHP, you can use the DateTime class and compare the dates using the comparison operators.

Here’s an example code snippet that shows how to check if a date is between two other dates:

$startDate = new DateTime('2022-01-01');
$endDate = new DateTime('2022-12-31');
$dateToCheck = new DateTime('2022-05-01');

if ($dateToCheck >= $startDate && $dateToCheck <= $endDate) {
    echo "The date is between the start and end dates.";
} else {
    echo "The date is not between the start and end dates.";
}

In this example, we have three DateTime objects representing the start date, end date, and the date to check. We then use the comparison operators (“>=”, “<=”, and “&&”) to check if the date to check falls between the start and end dates.

If the date to check is between the start and end dates, the output will be “The date is between the start and end dates.” Otherwise, the output will be “The date is not between the start and end dates.”

You can modify this code to use different date formats and adjust the comparison logic as needed.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.