Home  >  Article  >  Backend Development  >  How to Determine if a Date Falls Between Two Dates in PHP?

How to Determine if a Date Falls Between Two Dates in PHP?

DDD
DDDOriginal
2024-11-16 05:59:03387browse

How to Determine if a Date Falls Between Two Dates in PHP?

PHP Check if a Date Falls Between Two Dates

To verify if a particular date falls within a specified range, the PHP strtotime() function plays a crucial role.

Here's an updated version of your code that incorporates the strtotime() function:

$paymentDate = date('Y-m-d');
$paymentDate=date('Y-m-d', strtotime($paymentDate));
$contractDateBegin = date('Y-m-d', strtotime("01/01/2001"));
$contractDateEnd = date('Y-m-d', strtotime("01/01/2012"));

if (($paymentDate >= $contractDateBegin) && ($paymentDate <= $contractDateEnd)){
    echo "is between";
}else{
    echo "NO GO!";  
}

Using strtotime(), we convert all dates to the "Y-m-d" format for consistency. Additionally, the comparison operators are modified to ">=" and "<=" to account for potentially including today's date in the interval.

By implementing these changes, your code should accurately determine whether the provided date falls within the specified date range.

The above is the detailed content of How to Determine if a Date Falls Between Two Dates in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn