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
https://forums.phpfreaks.com/topic/77891-solved-compare-dates/
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
https://forums.phpfreaks.com/topic/77891-solved-compare-dates/#findComment-394337
Share on other sites

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.

 

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

 

http://code-bin.homedns.org/62

 

This what you're looking for?

Link to comment
https://forums.phpfreaks.com/topic/77891-solved-compare-dates/#findComment-394420
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.