我在数据库表departureDateTime and arrivalDateTime
中有两个字段和类似的值
departureDateTime=03/18/2012 1:05 PM
arrivalDateTime=03/18/2012 3:15 PM
我桌上有几百张唱片。
我需要根据这两个时间的持续时间进行排序和显示。我知道从两个日期计算持续时间
Duration=(strtotime($arrivalDateTime') - strtotime($departureDateTime))/3600
但是我如何编写一个mysql查询来排序和显示数据库中的这100条记录
有人知道吗?
感谢
将departureDateTime更改为DATETIME,而不是varchar或您当前使用的任何内容。然后你会使用这样的东西:
SELECT other, stuff, TIMEDIFF(departureDateTime, arrivalDateTime) as theDifference FROM myTable ORDER BY theDifference ASC LIMIT 0, 100
如果将日期和时间保持在日期-时间列类型中,则不会出现此问题。考虑改变。
还有mysql函数可以计算日期和时间http://dev.mysql.com/doc/refman/5.6/en/datetime.html
如果没有,您可以使用usort在PHP中进行排序。