Jump to content

Compare date_time field with current date


elpaisa

Recommended Posts

Hi,

 

I have an if statement tha is executed when the date in a DATE_TIME field in a database y is older than today

 

heres is the code,

 

	

         $date = time();
        if($user['exp_date'] >= $date)
       {  
          $error[] = '<script language="javascript" type="text/javascript">alert(\'YOUR ACCOUNT HAS EXPIRED PLEASE RENEW YOUR MEMBERSHIP\');</script>';
        }

 

id doesn't work, the date time format in the database appears so: 2008-03-18 22:06:15, how do get the current time and compare it with the database field

 

hello,

 

you could convert both to a unix timestamp, should ork then:

 

         $date = time();
        if(strtotime($user['exp_date']) >= $date)
       {  
          $error[] = '<script language="javascript" type="text/javascript">alert(\'YOUR ACCOUNT HAS EXPIRED PLEASE RENEW YOUR MEMBERSHIP\');</script>';
        }

 

hope this helps,

Just a general word of advice...

 

I see lots of people trying to store text-dates with their applications...timestamps are almost always a better option.  It's very easy to convert them back into "readable" format for display and they're very easy to compare/manipulate/etc.

<?php
$one_month_from_now = mktime(date('h'),date('i'),date('s'),date('m')+1,date('d'),date('y'));

echo date('h:i:s m/d/y',$one_month_from_now);
?>

 

here is how ya do one month from now. It looks messy, but it is actually really simple. it is just a combination of date() and mktime().  They can be a bit confusing at first but learn them, love them, and use them and date, time manipulation in PHP will be a breeze.

 

Nate

He is using MySQL DateTime, not text.

 

Just a general word of advice...

 

I see lots of people trying to store text-dates with their applications...timestamps are almost always a better option.  It's very easy to convert them back into "readable" format for display and they're very easy to compare/manipulate/etc.

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.