NVix
NVix

Reputation: 15

PHP date() - error in finding date in between

I'm using the following code to see if a date falls between 2 other dates.

    public function dateCompare($date1, $date2) 
    {

    $interimDate = date('d/m/Y');
    $StartDate = DateTime::createFromFormat('d/m/Y', $date1);
    $EndDate = DateTime::createFromFormat('d/m/Y', $date2);



    if ($interimDate > $StartDate && $interimDate < $EndDate)
    {
      echo 'Falls during given period';
    }
    else {
        echo 'Does not fall during given period';
    }

The two dates passed as follows

dateCompare('01/08/14', '30/12/14');

For some reason I continually get the message that todays date does not fall between the given period. I have checked the servers datetime and it is correct. Is anyone able to point out what exactly is causing the error?

Upvotes: 0

Views: 38

Answers (1)

tigrang
tigrang

Reputation: 6767

You have a 2 letter years, so it should be lower case y for your format: d/m/y. Also, make $interimDate equal to a new DateTime() object so you can compare properly.

Upvotes: 1

Related Questions