Reputation: 1
I'm making a news feed which stores dates as a string like Tuesday 05 June 2012 04:14:44 PM
.
I want to do two things.
Upvotes: 0
Views: 142
Reputation: 1114
I guess this will work for your problem, yet i advise you to modify your table design if possible.
$result = mysql_query("SELECT * FROM newsfeed");
$records = array();
$i = 0;
while ($row = mysql_fetch_array($result))
{
$records[$i] = array('news_idPK' => $row['news_idPK'],
'date' => date("Y-m-d H:i:s", strtotime($row['date'])),
'news' => $row['news']
);
$i++;
}
usort($records, "cmp");
function cmp($a, $b) {
$a = strtotime($a['date']);
$b = strtotime($b['date']);
return $a - $b;
}
$ctr = count($records)-1;
for($x=$ctr; $x>=0; $x--)
{
$newsDate = date("l d F Y h:i:s A", strtotime($records[$x]['date']));
if(date("Y-m-d", strtotime($records[$x]['date'])) == date("Y-m-d"))
{
$newsDate = date("h:i:s A",strtotime($records[$x]['date']));
}
echo $records[$x]['news_idPK']." - ".$newsDate." - ".$records[$x]['news']." <br/>";
}
Upvotes: 0
Reputation: 1341
I would change the storage to store some "unix timestamp". From that you can print out whatever format of date you like. And also, it's a number and is very easily sortable.
Upvotes: 3
Reputation: 132
This sorting method isn't effective. You can add an ID column and sort by ID. each time someone post a post , and it's inserted to the feed - a incremented ID will be inserted. it's parallel to date sorting - you want the newest.
it's the most recommended and effective sorting method.
Upvotes: -2