comparing two date variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mc3
Forum Newbie
Posts: 13
Joined: Thu Jan 24, 2008 5:31 pm

comparing two date variables

Post 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.";
 
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: comparing two date variables

Post 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
mc3
Forum Newbie
Posts: 13
Joined: Thu Jan 24, 2008 5:31 pm

Re: comparing two date variables

Post by mc3 »

Thanks very much. How can I create an integer from that object in order to compare it to the other integer?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: comparing two date variables

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: comparing two date variables

Post 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.
mc3
Forum Newbie
Posts: 13
Joined: Thu Jan 24, 2008 5:31 pm

Re: comparing two date variables

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: comparing two date variables

Post 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
}
?>
Post Reply