MattHamer5
MattHamer5

Reputation: 1491

Check if the date today is between a range, without using the year

I am trying to determine whether or not the current date is situated within a predetermined range of two different dates. What I currently have works fine; although I need it to ignore the year field, and just focus on the day and month, enabling it to work for future years without having to change the dates in the code each time.

$today = date('Y-m-d');
$today = date('Y-m-d', strtotime($today));

$winter = date('Y-m-d', strtotime("21/12/2020"));
$spring = date('Y-m-d', strtotime("20/06/2020"));

if( ($today >= $winter) && ($today <= $spring) ) {
  echo "Generic in timeframe message";
} else {
    echo "Not in timeframe";
}

Upvotes: 0

Views: 148

Answers (1)

ADyson
ADyson

Reputation: 62074

If you want it to always compare dates within the current year, you can just get PHP to tell you the current year, and use that in your winter/spring dates:

$yr = date("Y"); //get the current year in yyyy format
$winter = date('Y-m-d', strtotime("21/12/".$yr));
$spring = date('Y-m-d', strtotime("20/06/".$yr));

Upvotes: 1

Related Questions