[thelist] Logic help

Ray Hill lists at prydain.com
Thu May 2 21:20:01 CDT 2002


Woops, I forgot to include the code.  So here it is, all wrapped up in
a nice, reusable PHP fucntion.

Note that we're actually testing for non-winter dates and reversing
it, since winter spans the new year.  To make similar functions for
the other seasons, you would need to reverse the 'return true' and
'return false' order.

<?
function is_winter($date_to_test=false) {
  // Set the default value for $date_to_test.
  if (!$date_to_test) {
    $date_to_test = time();
  }

  // Reformat the timestamp to MMDD format.
  $date_to_test = date("md",$date_to_test);

  // Set the begin/end dates for the season in mmdd format.
  $date_season_begins = "1222";
  $date_season_ends = "0320";

  // Check to see if our test date is between the start/end dates or
not.
  if (($date_season_ends < $date_to_test) && ($date_to_test <
$date_season_begins)) {
    return false;
  } else {
    return true;
  }
}


// Now let's put the function to work.
if (is_winter(1020391487)) {
  print("It is winter.  Bundle up.<br>");
} else {
  print("It is not winter. Leave your coat behind.<br>");
}
?>


I'll also add a note about a potential bug here.  If you don't assign
the $season_begins and $season_ends variables as strings, and one of
them has a leading zero, it will reformat the one with the leading
zero to another number, and screw up your test.  I'm not sure why it
does that, but if you are going to assign them as numbers instead of
strings, do not put in a leading zero.


--ray





More information about the thelist mailing list