我想查看我的數據庫日期和時間,並將它們與當前日期和時間進行比較,並說出哪個日期和時間更長。Mysql比較存儲到今天日期的日期
<?php
include_once("db.php");
$date = new DateTime();
//echo $date->format('Y-m-d H:i:s')
$query = "SELECT timedate, email FROM mailer";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result)){
$tot = $row['timedate'];
$ema = $row['email'];
if($tot > $date) {
//echo "<tr><td>" .$row['timedate']."</td><td>";
echo"database dates higher then now dates" . "<br>";
echo "<tr><td>" .$row['email']."</td><td>" ."<br>";
echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
}
else {
echo"database dates lower then now dates" . "<br>";
echo "<tr><td>" .$row['email']."</td><td>". "<br>";
echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
}
}
mysql_close();
?>
下面是測試輸出,我得到:
database dates lower then now dates
[email protected]
2015-05-04 08:00:00
database dates lower then now dates
[email protected]
2015-05-04 00:00:00
database dates lower then now dates
[email protected]
2015-05-30 00:00:00
正如你所看到的最後一項是錯誤的,其設置爲5月30日,所以應該是高於現在的日期。我的猜測是我沒有正確使用當前日期,我需要格式化它。
'timedate'的數據類型是什麼? –
類型是日期時間 – alkokarko