Jump to content

How to make MYSQL query to sort the articles by date ??


Recommended Posts

Hi All,

I am struggling since last 3 hrs and yet not able to find a way to get this done. Let me describe my problem.

 

I am developing a site using PHP and MYSQL where i can post articles. Each Article has features like author, date, data etc... I want to find out top 5 recent articles.

 

I store the date of an article in database as  $timeStamp = Date("F j, Y");

 

when i make a query to retrieve the latest article by date as

 

$result = mysql_query("SELECT * FROM postData ORDER BY postDate DESC LIMIT 0, 5");

 

it doesn't give any output...

 

Can anyone figure our what is wrong ?

 

Provide me a way to get this done... Your quick reply will be appreciated.

 

Thanks in advance.

Easiest way is to store date as you get it from time(), e.g. as a unix timestamp. This is basically returns an int, which is great for comparing things to. Then when you wish to show the user, then you format it to a specific format, this is because it may be different depending upon timezone etc...

 

The way your doing it is ordering them by the the numeric value of the character string and the way they sum up is not in the same order as the way the months, days, etc are spelled! (if that's clear?)

Sample code for table creation:

CREATE TABLE tn { postDate int, etc };

 

Insert it:

$postDate = time();
$result = mysql_query("INSERT INTO tn VALUES (x, y, z, '$postDate'");

 

Query:

$result = mysql_query("SELECT * FROM postData ORDER BY postDate");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.