Trying to sort dates

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
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Trying to sort dates

Post by me! »

What I want to do is simple but I just cant get it to work :(

I have six event dates that I want to list on the page and distinguish when events have passed.
It is almost working like this but I would like to have the months but that did not work?

Showing the "Next" event would also be nice!

Code: Select all

<?php
			//put event show dates here...
				$date_1 = "5 13, 2007";			//first show
				$date_2 = "6 24, 2007";			//next show
				$date_3 = "7 15, 2007";			// ect...
				$date_4 = "8 5, 2007";
				$date_5 = "9 30, 2007";
				$date_6 = "10 14, 2007";
				
				$today = date("n d, Y");		// 10 21, 2007		
				
				if ($date_1 < $today) {echo "<span class=\"style1\">$date_1 - past</span><br />"; } else { echo "<b>$date_1</b><br />"; }
				if ($date_2 < $today) {echo "<span class=\"style1\">$date_2 - past</span><br />"; } else { echo "<b>$date_2</b><br />"; }
				if ($date_3 < $today) {echo "<span class=\"style1\">$date_3 - past</span><br />"; } else { echo "<b>$date_3</b><br />"; }
				if ($date_4 < $today) {echo "<span class=\"style1\">$date_4 - past</span><br />"; } else { echo "<b>$date_4</b><br />"; }
				if ($date_5 < $today) {echo "<span class=\"style1\">$date_5 - past</span><br />"; } else { echo "<b>$date_5</b><br />"; }
				if ($date_6 < $today) {echo "<span class=\"style1\">$date_6 - past</span><br />"; } else { echo "<b>$date_6</b><br />"; }
			?>
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

You want to sort the dates too, or just mark the old dates with " - past"?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is this information coming from a database? If so, are the dates stored in a DATE (or variant thereof) type? If so, you can sort them there. If it's not a DATE type, convert them to one. If it's not a database you'll need to standardize the date more (leading zeros) and reorder the date information so PHP can sort the information easily. Specifically the year must proceed the month which must proceed the day.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post by me! »

Don't want o sort them just sho when they are past.

And there is no db
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You still need to convert them to a uniform format so PHP can do comparisons. Right now, they aren't uniform.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Post by me! »

This is what I came up with:

Code: Select all

<?php
			//put show dates here...
			
				$date_1 = "May 13, 2007";			//Format "June 6, 2007"
				$date_2 = "June 24, 2007";			//next show
				$date_3 = "July 15, 2007";			// ect...
				$date_4 = "August 5, 2007";
				$date_5 = "September 30, 2007";
				$date_6 = "October 14, 2007";
				
				$sort_date_1 = "2007 05 13";			//Format Year "2007" Mounth "06" Day "06" 
				$sort_date_2 = "2007 06 24";			//next show
				$sort_date_3 = "2007 07 15";			// ect...
				$sort_date_4 = "2007 08 05";
				$sort_date_5 = "2007 09 30";
				$sort_date_6 = "2007 10 14";
				
			/////////////////////////don't edit past this line///////////////////
				
				$today = date("Y m d");		// 2007 10 21
				
				if ($sort_date_1 < $today) {echo "<span class=\"style1\">$date_1 - past</span><br />"; } else { echo "<b>$date_1</b><br />"; }
				if ($sort_date_2 < $today) {echo "<span class=\"style1\">$date_2 - past</span><br />"; } else { echo "<b>$date_2</b><br />"; }
				if ($sort_date_3 < $today) {echo "<span class=\"style1\">$date_3 - past</span><br />"; } else { echo "<b>$date_3</b><br />"; }
				if ($sort_date_4 < $today) {echo "<span class=\"style1\">$date_4 - past</span><br />"; } else { echo "<b>$date_4</b><br />"; }
				if ($sort_date_5 < $today) {echo "<span class=\"style1\">$date_5 - past</span><br />"; } else { echo "<b>$date_5</b><br />"; }
				if ($sort_date_6 < $today) {echo "<span class=\"style1\">$date_6 - past</span><br />"; } else { echo "<b>$date_6</b><br />"; }
			?>
Post Reply