astralaaron Posted May 18, 2007 Share Posted May 18, 2007 how do I order messages by the most recent date they were posted?I am making a little blog system, on the front page it is basically a table that displays the users that have blogs, and I want to display the date of the most recent post they have made and they should be orderd with the most recent poster on top..any advice? Link to comment Share on other sites More sharing options...
yoshida Posted May 18, 2007 Share Posted May 18, 2007 You can order stuff in your query by column name. GET * FROM blog ORDER BY date LIMIT 10 Limit ten is for when you want to limit the results to the ten newest posts, in this case.Hope this helps. Link to comment Share on other sites More sharing options...
Anders Moen Posted May 18, 2007 Share Posted May 18, 2007 GET * FROM blog ORDER BY date LIMIT 10 Ehh...it's not GET. It's SELECTSELECT * FROM blog ORDER BY date LIMIT 10 Link to comment Share on other sites More sharing options...
yoshida Posted May 18, 2007 Share Posted May 18, 2007 Sure... ; forgot to check the syntax. Day off... brain...melting...Don't forget to replace 'date' with the actual name of the data you want to sort by. Link to comment Share on other sites More sharing options...
astralaaron Posted May 18, 2007 Author Share Posted May 18, 2007 I just wanted to point out, when doing this to order by the most recent date you need to "SELECT * FROM table ORDER BY date DESC";if you dont descend it orders backwards in recent dates Link to comment Share on other sites More sharing options...
astralaaron Posted May 18, 2007 Author Share Posted May 18, 2007 ran into a problem, it does order by the most recent date / time. but it thinks 5:50 AM is more recent than right now, 1:55 PMmy date / time looks like this 18/05/07 05:55:19 (which was AM) and 18/05/07 01:55:30 (which is PM)the 5:55 date gets listed before 1:55.. does anyone get whats going wrong here?how can I get if it is am or PM so i can really order by the most recent date? Link to comment Share on other sites More sharing options...
Lulzim Posted May 18, 2007 Share Posted May 18, 2007 If you have the primary key field set to auto increment, just order by primary key SELECT * FROM table_name ORDER BY primary_key_field DESC and it will order them correctly.Remember, only if primary key field is auto increment. Link to comment Share on other sites More sharing options...
zppblood Posted May 18, 2007 Share Posted May 18, 2007 ran into a problem, it does order by the most recent date / time. but it thinks 5:50 AM is more recent than right now, 1:55 PMmy date / time looks like this 18/05/07 05:55:19 (which was AM) and 18/05/07 01:55:30 (which is PM)the 5:55 date gets listed before 1:55.. does anyone get whats going wrong here?how can I get if it is am or PM so i can really order by the most recent date?That could be because 5 > 1, add 12 to the time when it is PM so 1 becomes 13 which is > 5. Link to comment Share on other sites More sharing options...
astralaaron Posted May 18, 2007 Author Share Posted May 18, 2007 That could be because 5 > 1, add 12 to the time when it is PM so 1 becomes 13 which is > 5.yes I understand why it is doing that lol.. there is no determining what is am and what is PM. how do you do that time, 1:00 pm = 13:00 ? Link to comment Share on other sites More sharing options...
zppblood Posted May 18, 2007 Share Posted May 18, 2007 I would need to know how you store/retrieve the data. <?php $ap=Date("A"); if ($ap=="PM") { $hour=Date("g")+12; }?> Link to comment Share on other sites More sharing options...
astralaaron Posted May 19, 2007 Author Share Posted May 19, 2007 this is where the date comes from, please help me fix that if you can so it knows the difference between AM and PM when I order them! thanks :-) <?phpinclude('blogcheck.php');$blog_user = $_GET['user'];mysql_connect("localhost","root","******")or die("could not connect");mysql_select_db("t_blogs")or die("could not select db");$topic = $_POST['topic'];$detail = $_POST['body'];$date = date("d/m/y h:i:s");$sql = mysql_query("INSERT INTO {$blog_user}_topic (topic, detail, date) VALUES ('$topic', '$detail', '$date')");if (!$sql) {die('error: ' . mysql_error());} else { $sql2 = mysql_query("UPDATE bloggers SET post_date = '$date' WHERE name = '$blog_user'"); if (!$sql2) { die('error: ' . mysql_error()); }}header("location:http://localhost/tuneinordie/blog/panel.php?user=$blog_user");?> Link to comment Share on other sites More sharing options...
zppblood Posted May 19, 2007 Share Posted May 19, 2007 $date=date('d/m/y ');$h=date('h')+12;$date.=$h.date(':i:s'); Link to comment Share on other sites More sharing options...
astralaaron Posted May 19, 2007 Author Share Posted May 19, 2007 $date=date('d/m/y ');$h=date('h')+12;$date.=$h.date(':i:s');thanks, I will do that when I get home :-) Link to comment Share on other sites More sharing options...
astralaaron Posted May 19, 2007 Author Share Posted May 19, 2007 works perfect, thanks. Link to comment Share on other sites More sharing options...
astralaaron Posted May 19, 2007 Author Share Posted May 19, 2007 works perfect, thanks.this worked perfect I thought, untill it turned AM again.. it is the same problem but backwards!1 am = 13:001 pm will be 13:00 also...there has to be a way to do this.. anyone know?it should go 1:00 for am - 24:00 (13:00 for 1 pm) Link to comment Share on other sites More sharing options...
zppblood Posted May 19, 2007 Share Posted May 19, 2007 this worked perfect I thought, untill it turned AM again.. it is the same problem but backwards!1 am = 13:001 pm will be 13:00 also...there has to be a way to do this.. anyone know?it should go 1:00 for am - 24:00 (13:00 for 1 pm)My bad, you might want to add a conditional in there. $date=date('d/m/y ');$h=date('h');if (date("A")=="PM") { $h+=12;}$date.=$h.date(':i:s'); Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted May 20, 2007 Share Posted May 20, 2007 The easy way is this: $date = date("d/m/y H:i:s"); Note the capital H, which give you the hour is 24-format (00 through 23)http://php.net/manual/en/function.date.php Link to comment Share on other sites More sharing options...
astralaaron Posted May 20, 2007 Author Share Posted May 20, 2007 The easy way is this:$date = date("d/m/y H:i:s"); Note the capital H, which give you the hour is 24-format (00 through 23)http://php.net/manual/en/function.date.php haha thanks Link to comment Share on other sites More sharing options...
zppblood Posted May 20, 2007 Share Posted May 20, 2007 The easy way is this:$date = date("d/m/y H:i:s"); Note the capital H, which give you the hour is 24-format (00 through 23)http://php.net/manual/en/function.date.php I am an idiot lol. Link to comment Share on other sites More sharing options...
astralaaron Posted May 20, 2007 Author Share Posted May 20, 2007 I am an idiot lol.idiot? no way, your way still worked! Link to comment Share on other sites More sharing options...
zppblood Posted May 20, 2007 Share Posted May 20, 2007 idiot? no way, your way still worked!Thanks, but I still kind of feel like one because I should've known better. Link to comment Share on other sites More sharing options...
astralaaron Posted May 20, 2007 Author Share Posted May 20, 2007 Thanks, but I still kind of feel like one because I should've known better. well now we know Link to comment Share on other sites More sharing options...
justsomeguy Posted May 21, 2007 Share Posted May 21, 2007 The really easy way is just to store as an integer timestamp and not even worry about formatting. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.