1

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";
  }
Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
  • Winter ends in the next year. You need to set the end date to March of the year after. – Barmar Nov 18 '14 at 17:05
  • Well you're right. I've already read that. But my problem is that it gives me wrong value. – Robin Ryden Nov 18 '14 at 17:06
  • It looks like you're not using the actual start and end dates of the seasons, you're rounding off to the month. So make an array that maps each month number to the season. – Barmar Nov 18 '14 at 17:08
  • i just added the year (so i have d-m-y), and now it says "Höst" which is Autum! It seems that i worked this out thanks to @Barmar. Thanks all others too! – Robin Ryden Nov 18 '14 at 17:12
  • The way you use the `date` function is not correct. `date` accepts a format and a UNIX timestamp, i.e. `date("31-05")` is not valid, but `strtotime("2014-05-31")` would return a UNIX timestamp that you can use for comparison. However, see my answer below for a much better approach. – Ronni Egeriis Persson Nov 18 '14 at 17:19

3 Answers3

1

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";
kainaw
  • 4,256
  • 1
  • 18
  • 38
  • strtotime() is sketchy. It looks like the question was from a European locale and meant for the dates to be in DD-MM format. But, I cannot be certain that strtotime() in all locales would see it that way and not as YY-MM format. To avoid problems, I opt for mktime, which will make certain that days and months will not be confused. – kainaw Nov 18 '14 at 17:20
  • Okej, but this site is only going to be used in Sweden! :) With other words it will work with the code i wrote? :) – Robin Ryden Nov 18 '14 at 17:25
  • Yes. Instead of mktime, use strotime. It gives you a timestamp. Use the timestamp. Don't change it back to DD-MM format. – kainaw Nov 18 '14 at 17:34
1

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

$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

Ronni Egeriis Persson
  • 2,209
  • 1
  • 21
  • 43
0

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;
}
Syntax Error
  • 4,475
  • 2
  • 22
  • 33