Show date once (news script)

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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Show date once (news script)

Post by Jim »

Using a MySQL database to grab my news, I'd like to grab all news posts that were created on the same date and show them under a single date header.

How can I accomplish this? Much thanks :)
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

if you are using unix timestamps to store time than you can do a query to see if the dates fall within a certain range, namely 24hrs or 86400 seconds.
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post by PaTTeR »

You can use date/time function from MySQL - DATE_FORMAT() etc.
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post by PaTTeR »

Simple code

Code: Select all

<?php 
.................
$query = "SELECT DATE_FORMAT(date_ad,'%Y%m%d') AS date, news
          FROM news_table)";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result,MYSQL_ASSOC) &#123;
	$news&#1111;$row&#1111;'date']&#1111;] = $row&#1111;'news'];
&#125;

while (list($date,$new) = each($news)) &#123;
	echo $date.'<br />';
	while (list($key,$value) = each($new)) &#123;
		echo $value.'<br />';
	&#125;
	echo '<br />';
&#125;
?>
date_ad is date field in MySQL
news is new for this date
Post Reply