Page 1 of 1

Comparing dates from Mysql and PHP

Posted: Tue Nov 09, 2010 7:42 pm
by rolyestemonio
Hi, I am a PHP beginner, I am working this code and I need help, And kinda stack of it.
I have a database with 5 fields(id, date, time , title and description). This is what I want to do;
I want my script to go into the database fetch the date and compares dates if both days are equal, it should echo or read the dates as one and the titles and descriptions will be display with one dates that corresponds to the date taken from Mysql but when the dates are not equal it will display another dates. Your help is really highly appreciated guys.. Thanks in advance.

Example :

If dates are similar it will display in one category of dates

November 9, 2010 Updates

Sample 1
This is a description.


Sample 2
This is a description.


Sample 3
This is a description.

But if the dates is not equal it will display another date category

November 8, 2010 Updates

Sample 1
This is a description.


Sample 2
This is a description.

Any help please? What should i do after this codes?

Code: Select all

<?php
$result = mysql_query("SELECT * FROM tbl_localnews"); 
 while($rows = mysql_fetch_array($result) ){
 //If the dates in database is exist in many times.
//Display  it in one date category.
 if ( $rows > 1 ) { 
 ?>
  <h3 class="update">
  <em>News for <?php print $rows['date']; ?></em><em style="font-size:12px; float:right;">Updated <?php echo $rows['time'] ?></em></h3>
 <?php
  // If Not the same Display dates in another category.
  }else{?>
  <h3 class="update">
  <em>News for <?php print $rows['date']; ?></em><em style="font-size:12px; float:right;">Updated <?php echo $rows['time'] ?></em></h3>
  <?php 
 }
}
?>
But my codes does not work. It display all the dates even if it is similar dates. Can any one help me please.. :?:

Re: Comparing dates from Mysql and PHP

Posted: Wed Nov 10, 2010 10:24 pm
by s992
Well, this gets the job done but it looks like hell. I stored the dates as Unix timestamps in my DB because it was the only way that I could get them to sort properly.

Code: Select all

$result = mysql_query("SELECT * FROM tbl_localnews ORDER BY date DESC"); // Newer dates first
$dates = array(); // This is for later...
$num = mysql_numrows($result);
$i = 0;
while($i < $num) {
	$row = mysql_fetch_assoc($result);
	// Format the timestamp to something readable, this one is "January 01, 2010"
	$date = date("M d, Y",$row['date']);
	$time = $row['time'];
	// If this row's date is not already in the dates[] array, add it along with the time it points to:
	if( ! (in_array($date,$dates))) { 
		$dates[$date][$i] = $time;
	} else {
	/* If the date is already in there, we'll add this to that same date's key
	 * and then create a multidimensional array for all the times that fall under that date */
		$dates[$date][($i+1)] = $time; 
	}
	$i++;
}
// Loop through the first keys in the array - dates:
foreach($dates as $date => $value) { 
	echo "<h3>" . $date . "</h3>";
	// Loop through the second keys in the array - times:
	foreach($dates[$date] as $date => $time) { 
		echo $time . "<br />";
	}
}
// That was sloppy, but it works. Beer time.

Re: Comparing dates from Mysql and PHP

Posted: Thu Nov 11, 2010 9:34 pm
by rolyestemonio
Wow. It works bro. Your cool, I salute on your ideas. Thank you very much.
I have something to know bro, is it possible to combine all the data in one fields and it will display separately?

Example:

I have 3 fields (id, description, link) and i store this in the ff. withe a separator "|":

description fields

This is a description1 | This is a description2 | This is a description3 | This is a description4

link fields

Link1 | Link2 | Link3 | Link4

And it will display like this :

1. This is a description1
Link1

2. This is a description2
Link2

3. This is a description3
Link3

4. This is a description4
Link 4

Thanks in advance to your help bro.