[thelist] Logic help

Paul Cowan paul at wishlist.com.au
Thu May 2 20:11:00 CDT 2002


John Corry wrote:
> I'm having a hard time coming up with the logic for
> determining whether a date is in winter or not.

I don't know PHP, so I don't know their logical constructions, but I can
give you pseudocode. It all depends, of course, on your definition of "the
first day of winter", which is less than trivial. I'll assume that you want
winter day 1 to be the solstice, December 21 (ish), 1st day of spring to be
the equinox, March 21 (ish)

currentmonth = monthof(todaysdate)
currentday = dayof(todaysdate)

when currentmonth is:
	1, 2:
		winter
	3:
		if (currentday <= 21)
			winter
		else
			spring
	4, 5:
		winter
	6:
		if (currentday <= 21)
			spring
		else
			summer

... etc. Or, to look at it another way:

if (currentmonth = 12 and currentday >= 21)
	or (currentmonth = 1)
	or (currentmonth = 2)
	or (currentmonth = 3 and currentday < 21) then

	/* we're in winter */

elseif (currentmonth = 3)
	or (currentmonth = 4)
	or (currentmonth = 5)
	or (currentmonth = 6 and currentday < 21) then

	/* we're in spring */

...etc. This isn't the greatest code, but it should serve to illustrate the
point. Don't look for a complicated solution to a simple problem!

> That doesn't work because if it's february, winter began last year. If
> december, winter ends next year.

This confused me for a while. Stupid hemispheres.

Paul



More information about the thelist mailing list