Ordering datetime column using php

Discussion in 'PHP' started by Cinta April, Jun 27, 2008.

  1. #1
    How do I order a datetime column in mysql database from latest to oldest using php?
    Do i simply use "ORDER BY TIME"
     
    Cinta April, Jun 27, 2008 IP
  2. dannet

    dannet Well-Known Member

    Messages:
    864
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #2
    Yes, just using:

    ORDER BY time DESC
    PHP:
     
    dannet, Jun 27, 2008 IP
  3. Cinta April

    Cinta April Banned

    Messages:
    262
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    doesn't work the time displays in order only for some.
     
    Cinta April, Jun 27, 2008 IP
  4. okasha

    okasha Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this

    "select * from `tablename` order by `fieldname` desc"


    make sure to put ``


    best regards
     
    okasha, Jun 28, 2008 IP
  5. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Get in the habit of inserting a timestamp via php and then order by that field. The user date('',$timestamp) to format it to your liking after pulling it back out.
     
    itnashvilleCOM, Jun 28, 2008 IP
  6. David Pankhurst

    David Pankhurst Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Not sure if you're using a mySQL 'official' DATETIME value type, or something the program created, so here's some options:

    1) if DATETIME, the values in the field will look something like this:

    YYYY-MM-DD HH:MM:SS

    and you CAN sort (as already mentioned).

    2) since that's not the case, I'm guessing you have two fields, one for date, one for time - in that case sort this way:

    ORDER BY datefieldname,timefieldname DESC

    3) a third possibility is you have a field that isn't organized well - for example "February 7, 2007" instead of "2008-02-07" - if that's the case, there is no easy way to sort.
     
    David Pankhurst, Jun 28, 2008 IP