简体   繁体   中英

Check if todays date is between two other dates

I am trying to check if todays date is between START and STOP date of a period, Winter, summer, spring etc..

and if the todays date is between, lets say.. the winter period, it will set the $season variable to which period it is.

But for the moment it just gives me "01/01", i don't understand why..

Thanks for help! :)

$season = date("d-m");
$season = date("d-m", strtotime($season));


$startSummer = date("01-06");
$endSummer = date("31-08");

$startAutum = date("01-09");
$endAutum = date("30-11");

$startSpring = date("01-03");
$endSpring = date("31-05");

$startWinter = date("01-12");
$endWinter = date("28-02");

// start and stop, periods

// $startYear = date("d-m", strtotime($startYear));         $endYear = date("d-m", strtotime($endYear));
$startSummer = date("d-m", strtotime($startSummer));      $endSummer = date("d-m", strtotime($endSummer));
$startAutum = date("d-m", strtotime($startAutum));        $endAutum = date("d-m", strtotime($endAutum));
$startSpring = date("d-m", strtotime($startSpring));      $endSpring = date("d-m", strtotime($endSpring));
$startWinter = date("d-m", strtotime($startWinter));      $endWinter = date("d-m", strtotime($endWinter));

  if(($season > $startSummer) && ($season < $endSummer)){
    $season = "Sommar";
  }else if(($season > $startAutum) && ($season < $endAutum)){
    $season = "Höst";
  }else if(($season > $startSpring) && ($season < $endSpring)){
    $season = "Vår";
  }else if(($season > $startWinter) && ($season < $endWinter)){
    $season = "Vinter";
  }

You can stick with timestamps. Don't convert back to dates. You are making invalid comparisons such as the assumption that 30-01 is less than 28-02. The computer will compare the very first 3 to the 2 and tell you that 30-01 is CORRECTLY greater than 28-02. So...

$startSummer = mktime(0,0,0, 6, 1, 2000); // The year doesn't matter according to your code
$endSummer = mktime(0,0,0, 8, 31, 2000);

Now, is some date between those? Assume I am checking $month and $day...

$myday = mktime(0,0,0, $month, $day, 2000);
if($myday>=$startSummer && $myday<=$endSummer) $season = "Summer";

If you use DateTime object—which is by far the best approach—you are able to compare these with the regular comparators, eg:

$date1 = new DateTime('today');
$date2 = new DateTime('2014-04-04');

if ($date1 < $date2) echo 'Past';
else if ($date1 == $date2) echo 'Present';
else echo 'Future';

See documentation: http://php.net/manual/en/datetime.diff.php#example-2368

Remember that a variable can be overwritten - just as the year progresses through the seasons, your variable can as well - as long as we do it in order we'll end up on the correct one. This means we only have to test if our date is after the date that a season changes.

// Since we're testing today's date
// we use the current year timestamps
$year = date('Y');
$startSpring = strtotime("$year-03-01");
$startSummer = strtotime("$year-06-01");
$startAutum = strtotime("$year-09-01");
$startWinter = strtotime("$year-12-01");

$today = time();

// The year starts with Winter
$season = 'Winter';
if($today > $startSpring) $season = 'Spring'; // Past the 1st day of spring?
if($today > $startSummer) $season = 'Summer'; // Etc...
if($today > $startAutumn) $season = 'Autumn';
if($today > $startWinter) $season = 'Winter';

echo 'It is currently '.$season;

Here's the same logic cleaned up in a pretty function that will check any date for you and return the season:

// Accepts an optional unix timestamp
// Uses the current date by default
function getSeason($test_date=FALSE){
    $test_date = $test_date ? $test_date : time();

    // Use the year of the date we're testing
    $year = date('Y', $test_date);

    // The year starts with Winter
    $season = 'Winter';
    if($test_date > strtotime("$year-03-01")) $season = 'Spring'; // Past the 1st day of spring?
    if($test_date > strtotime("$year-06-01")) $season = 'Summer'; // Etc...
    if($test_date > strtotime("$year-09-01")) $season = 'Autumn';
    if($test_date > strtotime("$year-12-01")) $season = 'Winter';

    return $season;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM