开发者

How can I create dates/times and compare them in SQL?

开发者 https://www.devze.com 2022-12-23 03:15 出处:网络
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 an

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号