Find out if date is between two dates, ignoring year

前端 未结 4 1284
孤城傲影
孤城傲影 2021-01-18 12:20

I need to determine if a date (month and day) is between two other month/days.

I\'ve attached an image to this post that describes what I\'m trying to do. Basically

4条回答
  •  遇见更好的自我
    2021-01-18 13:08

    //must be 2 digit month, then 2 digit day, like mm-dd
    $low  = '03-15';
    $high = '11-22';
    $date = '02-01';
    
    $isBetween = $date >= $low && $date <= $high;
    var_dump($isBetween);
    

    I'm making use of lexicographic ordering, which is how php compares strings. The key point is to put the largest units on the left(months), and make sure to left-pad each numeric segment to the same length, using zeros to pad.

    If you don't yet have your dates as strings formatted like this, you can use date('m-d', $timestamp) function to achieve that.

提交回复
热议问题