添加2天并进行比较

时间:2014-01-07 09:08:13

标签: php date strtotime

我已经编写了一些代码来检查2天是否过了两天,但它似乎没有用。

<?php
date_default_timezone_set('Europe/Amsterdam');

$_connection = mysqli_connect("localhost", "root", "root", "theater") or die("Error: " . mysqli_error());

$query = "SELECT * FROM Reservering";
$result = mysqli_query($_connection, $query);

while($row = mysqli_fetch_assoc($result)) {
    $res = strtotime($row['ReserveringsDatum']);
    echo date('d-m-y', $res);
}

mysqli_close($_connection);
?>

$ row ['ReserveringsDatum']是一个如下所示的日期:“07-01-14”(示例)。

出于某种原因,echo date('d-m-y', $res);向我展示了“14-01-07”,这只是被逆转的日期和年份。

修改 对于那些想知道它是如何结束的人来说,这是工作代码:

<?php
date_default_timezone_set('Europe/Amsterdam');

$_connection = mysqli_connect("localhost", "root", "root", "theater") or die("Error: " . mysqli_error());

$query = "SELECT *, DATEDIFF(NOW(), DATE(ReserveringsDatum)) 'age' FROM Reservering WHERE DATEDIFF(NOW(), ReserveringsDatum) > 2 ";
$result = mysqli_query($_connection, $query);

while($row = mysqli_fetch_assoc($result)) {
    if($row['Betaalt'] == 'nee') {
        $query2 = "DELETE FROM Reservering WHERE `ReserveringID` = '".$row['ReserveringID']."'";
        mysqli_query($_connection, $query2);
    }
}

mysqli_close($_connection);


?>

4 个答案:

答案 0 :(得分:0)

试试这个

$res = strtotime($row['ReserveringsID']);
$limit = strtotime($row['ReserveringsID']) + strtotime("+2 day", $res);
var_dump($res); var_dump($limit);
if($limit > $res) {
    echo "2 days have passed";
} else {
    echo "2 days have not yet passed";
}

答案 1 :(得分:0)

我认为你需要解决你的问题。

如果你想检查$ res是否超过两天,你必须将限制与当前时间进行比较,而不是$ res。

$res = strtotime($row['ReserveringsID']);
$limit = strtotime('+2 days', $res);

if($limit < time()) {
    echo "2 days have passed";
} else {
    echo "2 days have not yet passed";
}

答案 2 :(得分:0)

你的问题就在这一行

$limit = strtotime($row['ReserveringsID']) + $2days;

您需要像这样添加2天

$res = strtotime($row['ReserveringsID']);
$limit = strtotime('+2 days', $res);

将日期添加2天的工作演示 https://eval.in/86632

How do I add 24 hours to a unix timestamp in php?

Adding days to a timestamp

答案 3 :(得分:0)

如评论中所述,我会在SQL中进行选择。不需要选择所有行并在PHP中过滤它们。它在SQL中做得更好。

简单示例:

SELECT *, DATEDIFF(NOW(), ReserveringsDatum) 'age' FROM Reservering WHERE DATEDIFF(NOW(), ReserveringsDatum) > 2 

确保reserveringsdatum是DATE,如果它是TIMESTAMP,则需要将其转换为DATE。

SELECT *, DATEDIFF(NOW(), DATE(ReserveringsDatum)) 'age' FROM Reservering WHERE DATEDIFF(NOW(), ReserveringsDatum) > 2