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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.