Reputation: 79
I'm trying to compare the current date with a date field returned by a MySQL query called "expiry_date". This field is a date field formatted as YYYY-MM-DD
I'm using If Else, to initialize a variable with the result as follows:
$today = $expiry_date = date("d-M-Y", strtotime("now"));
if ($row['expiry_date'] > $today)
{
$msg="Not Expired";
}
else
{
$msg="Expired";
}
This doesn't appear to work and I would appreciate any suggestions on how to fix this.
Upvotes: 3
Views: 6036
Reputation: 539
you have $today = $expiry_date =
which should be $today =
or $expiry_date =
$today = date("Y-m-d");
if (strtotime($row['expiry_date']) > $strtotime($today))
{
echo "not expired";
} else {
echo "expired";
}
Upvotes: 0
Reputation: 5332
$today = $expiry_date = new \Datetime();
$row_expiry_date = date_create_from_format('Y-m-d', $row['expiry_date']);
if ($row_expiry_date > $today)
{
$msg="Not Expired";
} else {
$msg="Expired";
}
Upvotes: 2
Reputation: 24645
You have two options, you can either format the dates the same and compare as strings
$today = $expiry_date = date("Y-m-d", strtotime("now"));
if ($row['expiry_date'] < $today)
or you can convert the row data to a unix timestamp for the comparison.
$today = $expiry_date = time();
if (strtotime($row['expiry_date']) < $today)
Upvotes: 0