PHP - 如何比较两个不同的日期时间?

时间:2016-10-06 16:00:59

标签: php

在数据库中为current_timestamp设置我正在创建一个脚本,每小时运行一次,比较数据库的时间,然后从本地服务器查看是否已经超过一个小时到目前为止,我有这个,但是困惑到哪里去从这里开始:

<?php
require('../config/dbconfig.php');
$sql = "SELECT * FROM stocks";
$result = mysqli_query($dbconfig,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $time = $row["TimeBought"];
}
$Time = date("Y-m-d H:i:s",$time);
$DateTime = time();
$NewTime = strtotime($Time);

我该怎么做?

1 个答案:

答案 0 :(得分:0)

最好使用DateTime对象而不是日期函数。

$now = new DateTime();
$date = new DateTime('2014-06-04');

if ($now > $date) {
    echo 'The date is in the past.';
} else {
    echo 'The date is in the future.';
}
相关问题