Jump to content

new DateTime comparison won't work - I give up - what am I missing?


gregaryb

Recommended Posts

This date comparison ALWAYS returns false and I have no idea why.

 

if ($dateNow > $dateExpiry)
	$objectAdvertSpace->expired = "1";
else
	$objectAdvertSpace->expired = "0";

The two dates are created like this:

$dateNow = new DateTime();
$dateExpiry = new DateTime($row["expiry_date"]);
$dateAdded = new DateTime($row["date_added"]);

The first instance of $row["expiry_date"] contains "2023-12-18"
The second instance of $row["expiry_date"] contains "2025-02-26"

Both are from two rows in a table in my MySQL database.
$dateNow contains "2024-02-27" assuming $dateNow->format("Y-m-d");

Why the bloody is the comparison ($dateNow > $dateExpiry) resulting in $objectAdvertSpace->expired = "0" in both cases????

What am I missing????

Link to comment
Share on other sites

If the second instance of $dateExpiry/"2025-02-26" is being evaluated in the if condition, it's going to go into the else clause because $dateNow is not greater than "2025-02-26".

Is that in a while loop? If so, then when the while loop is done, $objectAdvertSpace->expired is going to equal to "0" because the 'if' condition is going to evaluate the last $row["expiry_date"] in the while loop, which if it's "2025-02-26", $objectAdvertSpace->expired is going to be set to "0".

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...