I've written some code to check if 2 days have passed using 2 dates, but it does not seem to work.

<?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'] is a date which looks like this: "07-01-14" (example).

For some reason the echo date('d-m-y', $res); shows me "14-01-07", which is just the day and year being reversed.

Edit: For those wondering how it ended up, here is the working code:

<?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);


?>
有帮助吗?

解决方案

As said in the comment, i would make the selection within SQL. There is not need selecting all the rows and filter them in PHP. Its something better done in SQL.

Simple Example:

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

Make sure reserveringsdatum is a DATE, if its a TIMESTAMP you need to convert it to DATE.

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

其他提示

Try this

$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";
}

I think you need to fix you're condition.

If you want to check if $res is more than two days you have to compare limit to the current time, instead of $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";
}

Your problem is in this line

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

You need to add 2 days like this one

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

Working demo of adding 2 days to a date https://eval.in/86632

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

Adding days to a timestamp

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top