How can I create dates/times and compare them in SQL?
I'm trying to create a BBS using PHP 开发者_StackOverflowand SQL, and I want to make it so the topic with the most current post is on top of the topic list. what is a easy way to check if one date and time is more current than another?
From Sql using ORDER BY DESC
SELECT *
FROM MyTable
ORDER BY DateVal DESC
Other than that, please provide so table schema for us to work with, or what you have tried (in code) so we can have a look...
if their datatype is datetime then a simple greater than ( >
) will do
But you do not have to compare between dates, just sort them according to date with descending direction ..
ORDER BY [datefield] DESC
In SQL, you can use simple comparators (<
, >
, =
, etc) for DATETIME fields:
SELECT * FROM table t WHERE t.date < 'datevalue'
In PHP, I suggest that you convert your dates to UNIX timestamps using strtotime. After that conversion, you can use simple comparators to find out which date is newer.
精彩评论