在PHP变量中存储MySQL字段日期时间并向其添加1小时

时间:2014-06-15 05:55:42

标签: php mysql

使用MySQLi和PHP我有一个类似下面的查询,我需要将MySQL orderdate表中的newOrder字段(格式为Datetime,如2014-06-15 22:12:00)存入一个PHP变量,如$orderTime,然后在时间上加1小时。

<?PHP
include 'conconfig.php';
$con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$query = "select * from newOrder WHERE orderdate < NOW() ORDER BY id DESC LIMIT 1";

 if ($result = $con->query($query)) 
{
    if ($result->num_rows > 0) 
    {}
    else
    {
        $resultStr = 'Nothing found';
    }}
}
$con->close();

是否有可能像在while循环中一样?

$orderTime = $row['orderdate'];
$orderTime = $orderTime +1hour;

由于

1 个答案:

答案 0 :(得分:1)

使用date()strtotime()轻松完成:

$orderTime = date('Y-m-d H:i:s', strtotime($row['orderdate'] . '+ 1 hour'));

更新以回答评论中的问题:

检查$ordertime与当前时间之间的差异是否大于2小时:

if (time() - strtotime($orderTime) > 7200) {
    echo 'Difference is greater than 2 hours';
}
相关问题