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.
$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";
}
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)
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";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.