Jump to content

[SOLVED] Compare dates???


mdawg

Recommended Posts

Hi Everyone,

 

I'm new to this forum.  I hope this is the right place for my question.

 

Background:

I need to populate a variable with the correct season (fall, spring, summer, or winter).  I will use this to point to the correct image folder.

 

Here's what I have so far, but for some reason does not work correctly.  Any thoughts/suggestions would be great.  View source below...thanks in advance.

 

If you know of a better way (I know there is) please let me know. :)

 

<?php
$todayDate = date('y-m-d');
$currYear = date('y-');
$fallSeason = $currYear . '09-22';
$springSeason = $currYear . '03-21';
$winterSeason = $currYear . '12-22';
$summerSeason = $currYear . '06-21';
$season = '';

// convert date with strtotime() function
$today = strtotime($todayDate);
$fall = strtotime($fallSeason);
$spring = strtotime($springSeason);
$winter = strtotime($winterSeason);
$summer = strtotime($summerSeason);

if ($today > $fall) {
	$season = "fall";
} else if ($today > $spring) {
	$season = "spring";	
} else if ($today > $winter) {
	$season = "winter";	
} else {
	$season = "summer";	
}

// Everything below is for display only
if ($today > $fall) {
	echo ('<h1>It\'s true: fall</h1>');
} else {
	echo ('<h1>not fall</h1>');
}

if ($today > $spring) {
	echo ('<h1>It\'s true: spring</h1>');
} else {
	echo ('<h1>not spring</h1>');
}

if ($today > $winter) {
	echo ('<h1>It\'s true: winter</h1>');
} else {
	echo ('<h1>not winter</h1>');
}

if ($today > $summer) {
	echo ('<h1>It\'s true: summer</h1><hr>');
} else {
	echo ('<h1>not summer</h1><hr>');
}

echo ('<h1>Today\'s Date: ' . $today . '</h1>');
echo ('<h1>Today\'s Season: ' . $season . '</h1><hr>');
echo ('<h1>Fall: ' . $fall . '</h1>');
echo ('<h1>Spring: ' . $spring . '</h1>');
echo ('<h1>Winter: ' . $winter . '</h1>');
echo ('<h1>Summer: ' . $summer . '</h1>');
?>

Link to comment
Share on other sites

well seeing your code

 

if ($today > $fall) {
	echo ('<h1>It\'s true: fall</h1>');
} else {
	echo ('<h1>not fall</h1>');
}

 

you will have to specify a range like fall is between October to december otherwise looking at you code it would override. consider the following

 

if ($today > $fall && $today < $spring) {
	echo ('<h1>It\'s true: fall</h1>');
} else {
	echo ('<h1>not fall</h1>');
}

 

hope its helpful

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.