Eiolon Posted February 27, 2010 Share Posted February 27, 2010 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! Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 27, 2010 Share Posted February 27, 2010 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.'; } ?> Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 27, 2010 Share Posted February 27, 2010 @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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.