Datediff问题 - PHP,Mysql

时间:2014-07-31 00:37:46

标签: php mysql datediff

所以在我的数据库中我有 regdate (日期类型),但不管它是什么日期,代码都会一直返回1.

<?php
$con = mysqli_connect("localhost","root","","login");

if (mysqli_connect_error()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM users");
$row = $result->fetch_array();

$date1 = new DateTime($row['regdate']);
$date2 = new DateTime("now");
$interval = $date1->diff($date2);
echo "It has been " .$interval->d." days ";
//$interval is supposed to be difference between regdate and todays date

我希望我不会错过一些愚蠢的东西。谢谢你的回答。

解决

1 个答案:

答案 0 :(得分:1)

我相信这可能会更容易。

SELECT DATEDIFF(NOW(),$row['regdate']);

我觉得你需要一个while循环来查找所有记录。