PHP check if date between two dates

PHP check if date between two dates

Asked on December 28, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Use the php function named strtotime().

    $paymentDate = date('Y-m-d');
    $paymentDate=date('Y-m-d', strtotime($paymentDate));
    //echo $paymentDate; // echos today!
    $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!";
    }
    
    Answered on December 28, 2018.
    Add Comment

    Try to to use PHP’s DateTime object,Because we can not compare the date-strings. Have a look at the example .

    $paymentDate = new DateTime(); // Today
    echo $paymentDate->format('d/m/Y'); // echos today!
    $contractDateBegin = new DateTime('2001-01-01');
    $contractDateEnd = new DateTime('2015-01-01');
     
    if (
      $paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
      $paymentDate->getTimestamp() < $contractDateEnd->getTimestamp()){
      echo "is between";
    }else{
      echo "NO GO!";
    }
    
    Answered on December 28, 2018.
    Add Comment

    One can use this code , if they consider hours as a thing.

    $paymentDate = strtotime(date("Y-m-d H:i:s"));
    $contractDateBegin = strtotime("2014-01-22 12:42:00");
    $contractDateEnd = strtotime("2014-01-22 12:50:00");
     
    if($paymentDate > $contractDateBegin && $paymentDate < $contractDateEnd) {
        echo "is between";
    } else {
        echo "NO GO!";
    }
    
    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.