I am trying to compare two dates (one stored in the db) and today's date using PHP and outputing a "print" statement based on the result of the comparison. My expiration date is stored as "datetime" in the db. It is called through a query and assigned to the variable $expire. The code I have for the comparison is below, but it always returns false, regardless of the date.
<?
$todays_date = date("m-d-Y");
$today = strtotime($todays_date);
$expired = strtotime($expire);
if ($today > $expired) {
print "<a target='_blank' href='/pdfwriter/generate_certificate.php?member=".$memberID."'>Print Membership Certificate</a>";
}
else
{
print "<a href='registration/registration.aspx?registerType=1'>Membership Expired - Renew Today</a>";
}
?>
This is easier to do with DateTime()
since it make comparing the dates easier. No need to convert to timestamps or anything plus it takes daylight savings time into account. I also think your comparison operator is backwards.
$today = new DateTime();
$expires = new DateTime($expire);
if ($today < $expires)
{
print "<a target='_blank' href='/pdfwriter/generate_certificate.php?member=".$memberID."'>Print Membership Certificate</a>";
}
else
{
print "<a href='registration/registration.aspx?registerType=1'>Membership Expired - Renew Today</a>";
}
An other solution can be simple PHP date class.
It provides comparison methods as well as date addition subtraction and other useful functions all together.
For example, for date comparison:
$date = new simpleDate();
if($date->now()->compare($other_date)->isBefore())
{ ... }
else { ... }
You can check the library tutorial page for more examples . Please click here .
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.