Need Help with Date Subtraction/Addition

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
rileychilds
Forum Newbie
Posts: 14
Joined: Mon Feb 06, 2012 5:43 pm

Need Help with Date Subtraction/Addition

Post by rileychilds »

Hi! I'm working on a project for my school and i need help. I need to create a due date along with a script that will check to see if books are due and add a fine to a persons account. my curent code for checking if something is due:

Code: Select all

//books are out for 14 days
if (dateDiff($bookdue, $date) > 14 ) {
//patron fines pulled in earlier in the code
$fine = $patronfines +1
$finesql = "UPDATE patrons SET '$fine' WHERE code = $bookpatron";
mysql_query($finesql);
}
I still can't calculate the due date (subtracting dates)
If any one could give me help that would be well... helpful :mrgreen:
Thanks
Riley Childs
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need Help with Date Subtraction/Addition

Post by Celauran »

If you have PHP 5.3.x, take a look at DateTime::diff().
rileychilds
Forum Newbie
Posts: 14
Joined: Mon Feb 06, 2012 5:43 pm

Re: Need Help with Date Subtraction/Addition

Post by rileychilds »

How do i apply that to my mysql database by adding the integer to the fines column
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need Help with Date Subtraction/Addition

Post by Celauran »

What does your table look like? Is there a due date column?
rileychilds
Forum Newbie
Posts: 14
Joined: Mon Feb 06, 2012 5:43 pm

Re: Need Help with Date Subtraction/Addition

Post by rileychilds »

yes
It has 2 tables one for books (contains all information including who the books was checked out to and when it is due) and one for patrons (which has a bar code, name, and number of fines.) each fine is incremental ed by one
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need Help with Date Subtraction/Addition

Post by Celauran »

So when you're creating the INSERT query, you can use DateTime to set the current date, and DateTime::modify() to set the due date.

I don't know that I'd have a column for number of fines, but rather I'd calculate that on the fly be getting the number of books where due date < NOW().
rileychilds
Forum Newbie
Posts: 14
Joined: Mon Feb 06, 2012 5:43 pm

Re: Need Help with Date Subtraction/Addition

Post by rileychilds »

i need to figure out what day it is and if that day is fourteen days after the day of checkout, and if so add +1 to the fines column
Post Reply