0

As in the example i need to check if a date is include in a date range, the problem i just need to compare days and month not the year. thanks

$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));
}
  • `month-day` are sortable if zero-filled on the day. – Jared Farrish May 26 '17 at 11:56
  • Possible duplicate of [How to check if a date is in a given range?](https://stackoverflow.com/questions/976669/how-to-check-if-a-date-is-in-a-given-range) – bamtheboozle May 26 '17 at 12:00
  • for example, i just need to know if 21 nov is include or not in the range 1 Nov / 31 jan, without the years...( 21/11 include in 1/11 - 31/01 ?) –  May 26 '17 at 13:18

2 Answers2

1

Use the date() format nd (month concatenated to zero-filled day) to remove the year, then you have a 3-4 digit number you can compare that doesn't use the year of the of the dates:

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

    // Check that user date is between start & end
    return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}

In fact, you could pass in the format:

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

    // Check that user date is between start & end
    return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}

check_in_range('2017-05-30', '2017-09-12', '2015-08-06', 'nd') // true
check_in_range('2017-05-30', '2017-09-12', '2015-05-06', 'nd') // false

http://codepad.org/Usnxkr9d

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • for example, i just need to know if 21 nov is include or not in the range 1 Nov / 31 jan, without the years...( 21/11 include in 1/11 - 31/01 ?) –  May 26 '17 at 13:18
  • Do it month-day and it sorts correctly, so it can be used to check "betweeness". Both of the functions I include demonstrate that. – Jared Farrish May 26 '17 at 13:38
  • If you were wanting to compare a month and day (and don't know the year), simply add any year to the dates. It ignores them in the comparison, so simply add any recent year (like the current year) to the date string. – Jared Farrish May 26 '17 at 16:57
0

You use the PHP DateTime class for this. An example using your code:

    $start_date = '2009-06-17';
    $end_date = '2009-09-05';
    $x = new DateTime($start_date);
    $y = new DateTime($end_date);
    $dateDifference = $x->diff($y);
    // see difference in days using $dateDifference->d
    // see difference in months using $dateDifference->m
Sotiris Kiritsis
  • 3,178
  • 3
  • 23
  • 31
  • This explicitly does NOT answer the only question, which is how to check by ignoring the year in a date. – Jared Farrish May 26 '17 at 11:57
  • @JaredFarrish I'm not sure why this does not answer the question. I explicitly state how you can see the days and the months difference. – Sotiris Kiritsis May 26 '17 at 11:58
  • The question is asking for "how do I check if May 5 is between April 1 and July 31 irrespective of any year in the date". – Jared Farrish May 26 '17 at 12:10
  • for example, i just need to know if 21 nov is include or not in the range 1 Nov / 31 jan, without the years...( 21/11 include in 1/11 - 31/01 ?) –  May 26 '17 at 13:18