Page 1 of 1

Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 5:58 pm
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

Re: Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 6:08 pm
by Celauran
If you have PHP 5.3.x, take a look at DateTime::diff().

Re: Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 6:15 pm
by rileychilds
How do i apply that to my mysql database by adding the integer to the fines column

Re: Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 6:22 pm
by Celauran
What does your table look like? Is there a due date column?

Re: Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 6:25 pm
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

Re: Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 6:28 pm
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().

Re: Need Help with Date Subtraction/Addition

Posted: Mon Feb 06, 2012 6:40 pm
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