Setting a "season" variable from today's date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bugs1012
Forum Newbie
Posts: 5
Joined: Fri Jun 26, 2009 6:21 am

Setting a "season" variable from today's date

Post by Bugs1012 »

I need to set a variable that reflects the current season, based on today's date:

Dec 21-Mar 20 = "winter"
Mar 21-June 20 = "spring"
etc

i'm very new to PHP so would appreciate all help!

thanks
Bugs1012
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Setting a "season" variable from today's date

Post by Mark Baker »

Code: Select all

 
$dateString = '20-Dec-2009';
$date = strtotime($dateString);
 
$today = date('z',$date);
$leapYear = date('L',$date);
 
if (($today >= (79 + $leapYear)) && ($today < (171 + $leapYear))) {
    $season = 'Spring';
} elseif (($today >= (171 + $leapYear)) && ($today < (263 + $leapYear))) {
    $season = 'Summer';
} elseif (($today >= (263 + $leapYear)) && ($today < (354 + $leapYear))) {
    $season = 'Autumn';
} else {
    $season = 'Winter';
}
echo $dateString.' is in '.$season;
 
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Setting a "season" variable from today's date

Post by juma929 »

Hello,

Just knocked you something up but came to double Mark's submission, but allows for seasonal differences across the world. I would personally stick with Mark's as its smaller.

I notice you work on phpexcel etc, I couldnt find a PHP4 version of that work for a client recently so had to build a version for PHP4, did I miss something or is PHP5 upwards all you currently support?

Thanks.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Setting a "season" variable from today's date

Post by Mark Baker »

juma929 wrote:I notice you work on phpexcel etc, I couldnt find a PHP4 version of that work for a client recently so had to build a version for PHP4, did I miss something or is PHP5 upwards all you currently support?
PHPExcel requires PHP 5.2 or above (it may work with earlier versions, but you can assume PHP5 as a minimum, and hasn't been tested with earlier), with php_zip enabled. That's really a minimum, although I've been looking for alternatives to php_zip. We also have a couple of minor "kludges" for variant support, such as internal implementation of functions such as acosh, asinh, atanh and money_format for platforms that don't have this functionality (Windows).
My development servers are running a mix of 5.25, 5.26 and 5.29 at the moment, so I can't guarantee it will work with earlier versions, though I do always try to avoid functions added in later releases of PHP, or to provide alternative methods.


Nor are we likely to provide PHP4 support, when most servers should be running the more recent versions of PHP; and we're now looking toward PHP6. Much of the new functionality that we're adding (e.g. basic charts are now implemented) and to improve performance, really requires the more recent versions of PHP.
Bugs1012
Forum Newbie
Posts: 5
Joined: Fri Jun 26, 2009 6:21 am

Re: Setting a "season" variable from today's date

Post by Bugs1012 »

Mark - thanks very much for this script. It works perfectly except i can't figure out how to get it to run with the actual date (not the variable you added). I'm sure i'm doing something really dumb and hope you can help!
thanks
Bugs
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Setting a "season" variable from today's date

Post by Mark Baker »

Bugs1012 wrote:Mark - thanks very much for this script. It works perfectly except i can't figure out how to get it to run with the actual date (not the variable you added). I'm sure i'm doing something really dumb and hope you can help!
thanks
Bugs
Default for the date function is the current date/time

Code: Select all

 
$today = date('z');
$leapYear = date('L');
 
if (($today >= (79 + $leapYear)) && ($today < (171 + $leapYear))) {
    $season = 'Spring';
} elseif (($today >= (171 + $leapYear)) && ($today < (263 + $leapYear))) {
    $season = 'Summer';
} elseif (($today >= (263 + $leapYear)) && ($today < (354 + $leapYear))) {
    $season = 'Autumn';
} else {
    $season = 'Winter';
}
echo 'Today is in '.$season;
 
Memo to self - somewhere at home I have a PHP port of the actual algorithm used for calculating Solistice and Equinox for a known Lat/Long. Perhaps (over the weekend) I'll have a go at using that to give you exact Spring/Summer/Autumn/Winter based on the server location/timezone.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Setting a "season" variable from today's date

Post by juma929 »

Hello,

Thanks for getting back to me about the versions you support :). I built a version that also supports charts in PHP4, I may post it online after tidying it up a little for people who cant run using the PHP 5 version, may even just send it directly to you to take a look at to provide to any users using an older PHP version? The reason I required PHP 4 support was due to the environment PHP was being run in for a particular client, so I had to go ahead and tackle the creation of excel files with charting support etc.

The class(s) I built handle all of the Open XML creation and zip/rename to form the XLSX files.

Anyway im going off topic again :P. Thanks for getting back.

Justin
Post Reply