Date Mathematics

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
BZorch
Forum Commoner
Posts: 45
Joined: Mon May 02, 2005 10:42 pm

Date Mathematics

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
BZorch
Forum Commoner
Posts: 45
Joined: Mon May 02, 2005 10:42 pm

Post 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.
Post Reply