Search code examples
mysqlunix-timestamp

MySQL does not order correctly using UNIX timestamp


I have this php code:

(from my database class)

$q = "SELECT * FROM" . CON_TBL. " WHERE a = $b ORDER BY thedate DESC";
$result = mysqli_query($this->connection, $q);

The dates (unix timestamp) on the DB are:

1138322340
1617584160
1617673680
759952800

I got this result:

30-Jan-1994 12:00
05-Apr-2021 20:48
04-Apr-2021 19:56
26-Jan-2006 18:39

The first date should be the last:

05-Apr-2021 20:48
04-Apr-2021 19:56
26-Jan-2006 18:39
30-Jan-1994 12:00

Any ideas what's going on?


Solution

  • The thedate column was a VARCHAR instead of an INT, once I changed the column to INT the ORDER was corrected.