Jump to content

Expiration dates


Eiolon

Recommended Posts

I want to make it show that a card has expired if it's after the expiration date.  Dates are stored in DATETIME format in the database so this is what I have done:

 

<?php $today = date("Y-m-d"); ?>

Status: <?php if ($ExpirationDate < $Today) {
echo 'The card has expired.';
} else {
echo 'Active.';
}
?>

 

It seems that my card is never expired even though I set the date in the database to be: 2010-02-26 00:00:00

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/193581-expiration-dates/
Share on other sites

Not surprising, you're not comparing integers, you're comparing formatted dates. PHP can't handle that comparison with <.

 

Instead, use strtotime on the variables and then compare:

 

<?php $today = time(); $CompareExpirationDate = strtotime($ExpirationDate); ?>

Status: <?php if ($CompareExpirationDate < $Today) {
echo 'The card has expired.';
} else {
echo 'Active.';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/193581-expiration-dates/#findComment-1019091
Share on other sites

@ialso agree, you are correct that the formatted date is the problem but not about the comparison operator.  The operator will work perfectly well but will do a string compare not a numeric compare, which will not deliver the required results.

 

The solution to convert dates to timestamps before conversion is, however, the way to go to ensure accuracy.

Link to comment
https://forums.phpfreaks.com/topic/193581-expiration-dates/#findComment-1019099
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.