我在mysql表中有两个日期。
sample:
date1: 2011-01-01 06:40:00
date2: 2011-02-19 18:00:00
我需要返回它们之间的区别,如下所示:
50天,12小时,20分钟
我如何在PHP或MYSQL中执行此操作?
答案 0 :(得分:4)
此代码应该满足您的需求:
<?php
$result = mysql_query("SELECT (date1, date2) FROM myTable;");
while (list($date1, $date2) = mysql_fetch_array($result)) {
$firstdate = new DateTime($date1);
$seconddate = new DateTime($date2);
$diff = $firstdate->diff($seconddate);
echo $diff->d . " days, " . $diff->h . " hours, " . $diff->i . "minutes\n";
}
?>
答案 1 :(得分:3)
试试这个,
SELECT CONCAT(
FLOOR(TIMESTAMPDIFF(HOUR,'2011-01-01 06:40:00', '2011-02-19 18:00:00') / 24), ' days ',
MOD(TIMESTAMPDIFF(HOUR,'2011-01-01 06:40:00', '2011-02-19 18:00:00'), 24), ' hours ',
MINUTE(TIMESTAMPDIFF(second,'2011-01-01 06:40:00', '2011-02-19 18:00:00')), ' minutes')
答案 2 :(得分:0)
答案 3 :(得分:0)
您可以阅读有关mysql DATEDIFF函数的信息:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff