0

I'm trying this one out and it's been a while since I last coded in PHP.

I have a function that checks if given date is in between the range of dates:

pr($this->isDateBetween("2014-11-15", "2014-12-05", "2014-06-14"));

public function isDateBetween($dt_start, $dt_check, $dt_end){
        if(strtotime($dt_check) >= strtotime($dt_start) && strtotime($dt_check) <= strtotime($dt_end)){
            return true;
        } else{
            return false;
        }
    }

But seems like I'm getting false. The expected result should be true because 2014-12-05 is less than 2014-06-14 .

December is less than June so that should be true?

Also, When I change 2014-06-14 to 2014-12-14 it became true.

Sorry, I'm not updated anymore with PHP. Please bear with me.

EDITS

Tried converting those to strtotime but still getting wrong results

rukia_kuchiki_21
  • 1,279
  • 3
  • 17
  • 34
  • 2
    You should probably find out which of the `dt_start` and `dt_end` is largest/smallest, then do an IF statement. The way you're doing it now, the order you've specified your arguments means no dates will be accepted. e.g. `2014-11-15` is your start date, but it is **after** `2015-06-14` which is your end date. – Jonathon Dec 04 '14 at 17:18
  • Additionally, the middle parameter i.e. the date you are checking is outside of the range specified by the other parameters. – Jonathon Dec 04 '14 at 17:23
  • Thanks I was confused. I need to study dates in a hard way but not thinking ahead – rukia_kuchiki_21 Dec 04 '14 at 17:24
  • you are misusing your method :) just swap the dt_start and dt_end – Hmmm Dec 04 '14 at 17:26

3 Answers3

0

The solution exist here:

How to check if a date is in a given range?

SOLUTION:

Converting them to timestamps is the way to go alright, using strtotime, e.g.

$start_date = '2009-06-17';

$end_date = '2009-09-05';

$date_from_user = '2009-08-28';

check_in_range($start_date, $end_date, $date_from_user);


function check_in_range($start_date, $end_date, $date_from_user)
{
  // Convert to timestamp
  $start_ts = strtotime($start_date);
  $end_ts = strtotime($end_date);
  $user_ts = strtotime($date_from_user);

  // Check that user date is between start & end
  return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}
Community
  • 1
  • 1
MartaGom
  • 501
  • 6
  • 27
0

The expected result should be true because 2014-12-05 is less than 2014-06-14 .

Totally wrong. The code IS Working. June 14th is "less" than December 12th. So your Dec 12th date is NOT between the start/end dates. it's past the end of the range.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

The expected result should be true because 2014-12-05 is less than 2014-06-14 .

Absolutely not. The expected result is not true. It is false, because "2014-12-05" is not less than "2014-06-14". December less than June? Never heard of such a peculiar logic before.

Also, When I change 2014-06-14 to 2014-12-14 it became true.

That's right. Because "2014-12-05" is definitely less than "2014-12-14", it is true. 5 December is obviously smaller than 14 December.

This has nothing to do with PHP versions or PHP built-in methods. This is all about your logic.

I think this is what you're looking for:

pr($this->isDateBetween("2014-06-14", "2014-11-15", "2014-12-05"));

public function isDateBetween($dt_start, $dt_check, $dt_end){
        if(strtotime($dt_check) >= strtotime($dt_start) && strtotime($dt_check) <= strtotime($dt_end)){
            return true;
        } else{
            return false;
        }
}

This will return true.

user229044
  • 232,980
  • 40
  • 330
  • 338