Link to home
Start Free TrialLog in
Avatar of sabecs
sabecs

asked on

Convert MySQL date with PHP and check if date has passed.

Hi,
I have an expiry date - MySQL date format 2006-07-14,
can anyone please tell how I can convert this and check if the date has passed or expired with PHP.

Thanks
Avatar of rbartz
rbartz

This should work easy and fast:

$result=mysql_query("SELECT UNIX_TIMESTAMP(expiry_date) as 'expire_date' FROM...... WHERE....  LIMIT 1");
Do the query, get the array and use:

$expires = $result[expire_date];  //gives us unix timestamp  of expiry date

$rightnow = time();  // gives us unix timestamp for rightnow

if ($expires > $rightnow) {
   echo "EXPIRED!";
} else {
   echo "Current!";
}

R
Sorry, got the if else logic backward, my fingers are tired.

EXPIRED and Current are reversed!

Or use if ($expires < $rightnow) {

Avatar of sabecs

ASKER

Thanks for your response, but I would prefer not to use a mysql query to format the date.
I already have a PHP variable set as a my date.
$expdate = '2006-07-14';
ASKER CERTIFIED SOLUTION
Avatar of rbartz
rbartz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it is usually a good idea to do the comparison on the db side
select * from table1 where id=$id and thedate < $expired
if a row returned, then it is not expired, if none, then expired
Avatar of sabecs

ASKER

Thanks rbartz, thats exactly what I was after.