Page 1 of 1

Date Mathematics

Posted: Mon Jun 12, 2006 8:26 pm
by BZorch
I have been trying over the last 24hrs to figure out how to do simple mathematics with dates. I am getting desperate. All of the examples I have reviewed seem so simple, but I am just not getting it. Basically, I have used the Now() function to record a timestamp in my MySQL database when someone signs in and set the number of downloads to 0. I then want to record their downloads. This is working fine, but once they sign in again, it sets the counter all over again because the date formula is getting a Null value (or at least that is what I think is happening). I want to limit the amount of downloads a user can perform each day.


Here is what I have done so far

Code: Select all

$first_date  = getdate( ); 
$second_date=getdate($row[1]);    //$row[1] is returned in the mysql query, it is the timestamp using Now() and the field is in timestamp format
$days_between = $second_date['yday'] - $first_date['yday'];
					
if ($days_between==0) {
						
echo "";//do nothing 
						
}else{
echo "Reset the download counter";// if this is a different day reset the download counter
}



Any help would be appreciated.

Posted: Mon Jun 12, 2006 9:22 pm
by feyd
Look through the Code Snippets board... there's a snippet in there (at least there was) that will tell you how much time has passed between two timestamps.

Posted: Tue Jun 13, 2006 3:38 am
by twigletmac
You can also do the calculation in MySQL itself using the date and time functions:
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html

Mac

Posted: Tue Jun 13, 2006 6:49 am
by BZorch
I could not find the code snippet that you directed me toward. I did try the MySQL route and it worked flawless (at least so far) using the following

Code: Select all

SELECT TO_DAYS(CURDATE()) - TO_DAYS(user.last_login)

Thank you for both for responding.