Page 1 of 1

comparing two date variables

Posted: Wed Jan 30, 2008 10:45 am
by mc3
I'm trying to compare two dates and then creating an if/then clause depending on the outcome. But it seems like there must be something I'm missing because

FYI, my 'activatetime' field in my database is an integer of 8 digits representing a date expressed as mmddyyyy.

Code: Select all

 
//get activatetime
 
$activatetime = mysql_query("SELECT activatetime FROM seens_dl WHERE user = '$user'");
 
$checkactivatetime = mysql_fetch_object($activatetime);
 
 
//set cutoff date
$cutoffdate = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
 
echo date("mdY", $cutoffdate);
 
 
//check if activatetime is before cutoff
 
if($checkactivatetime < $cutoffdate)
    echo "Sorry, this username was activated more than 3 days ago and can not be used to download anymore.";
 
 

Re: comparing two date variables

Posted: Wed Jan 30, 2008 10:48 am
by RobertGonzalez
What is the problem with what you have?

ACtually, nevermind. mysql_fetch_object() returns an object. You are comparing an object to an integer. That is your problem.
The PHP Manual on mysql_fetch_object wrote:mysql_fetch_object — Fetch a result row as an object

Re: comparing two date variables

Posted: Wed Jan 30, 2008 10:56 am
by mc3
Thanks very much. How can I create an integer from that object in order to compare it to the other integer?

Re: comparing two date variables

Posted: Wed Jan 30, 2008 11:02 am
by JAM
Perhaps;

Code: Select all

if ($checkactivatetime->activatetime < cutoffdate) {
Take a look at the phpmanual for mysql_fetch_object(). The first example should explain enough.

Re: comparing two date variables

Posted: Wed Jan 30, 2008 11:03 am
by RobertGonzalez
You use the field name as a property of the object (in the code you posted):

Code: Select all

<?php
//get activatetime
$activatetime = mysql_query("SELECT activatetime FROM seens_dl WHERE user = '$user'");
$checkactivatetime = mysql_fetch_object($activatetime);
 
//set cutoff date
$cutoffdate = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
 
if($checkactivatetime->activatetime < $cutoffdate) {
    echo "Sorry, this username was activated more than 3 days ago and can not be used to download anymore.";
}
?>
You could also do something like:

Code: Select all

<?php
//get activatetime
$activatetime = mysql_query("SELECT activatetime FROM seens_dl WHERE user = '$user'");
$checkactivatetime = mysql_fetch_row($activatetime);
 
//set cutoff date
$cutoffdate = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
 
if($checkactivatetime[0]['activatetime'] < $cutoffdate) {
    echo "Sorry, this username was activated more than 3 days ago and can not be used to download anymore.";
}
?>
or

Code: Select all

<?php
//get activatetime
$activatetime = mysql_query("SELECT activatetime FROM seens_dl WHERE user = '$user'");
$checkactivatetime = mysql_fetch_array($activatetime);
 
//set cutoff date
$cutoffdate = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
 
if($checkactivatetime['activatetime'] < $cutoffdate) {
    echo "Sorry, this username was activated more than 3 days ago and can not be used to download anymore.";
}
?>
Hope that helps.

Re: comparing two date variables

Posted: Wed Jan 30, 2008 11:55 am
by mc3
Thanks for the advice. I went ahead and changed the code to include the field name and now it looks like I'm getting and integer but somehow my comparison isn't working. And yet, my if/then clause seems to be giving a 'true' result.

What's being displayed is below. You can see that the activatetime is a bigger number than the cutoffdate. Any thoughts?



stdClass Object ( [activatetime] => 01302008 )

1

01272008Sorry, this username was activated more than 3 days ago and can not be used to download anymore.

Re: comparing two date variables

Posted: Wed Jan 30, 2008 12:48 pm
by RobertGonzalez
I think you are going to have to rethink your logic in terms of the dates you are using for comparison. Perhaps you could do your check in the database instead, and change the current mmddyyyy field to a regular datetime field?

I only bring this up because date math the way you are doing it could have wierd results when you look at the numbers you could be using.

As an example of using a datetime field (depending on your MySQL version of course):

Code: Select all

$sql = "SELECT COUNT(*) AS `user_active` 
        FROM seens_dl 
        WHERE user = '$user' 
        AND NOW() < DATE_ADD(`activatetime`, INTERVAL 3 DAY)";
 
$result = mysql_query($sql) or die(mysql_error()); // die() is only for testing, remove or handle for production
$row = mysql_fetch_array($result);
 
if ($row['user_active']) {
  // user is good
} else {
  // user is bad
}
?>