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

 

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

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.