Wednesday, July 14, 2010

PHP date??

My database puts out the date of:





2008-01-01 (date borrowed)





How can I get it to check whether the date borrowed plus the rental period (21 days) is greater than the todays date?

PHP date??
If you are using mysql then you could add the following clause to your query:





'WHERE DATE_ADD(date_borrow, INTERVAL 21 DAY) %26gt; NOW()'





This should then only return the results you want.
Reply:In simple terms you should be able to access the database using


where (date_borrowed + 21) %26gt; current date





But then this is database and language specific.


The 21 is days. You need to identify this as days. There should be a days function or a days keyword.





Current date should also be a current date function. This could be today() or now().





Without knowing what database you are using it is difficult to answer. This should be a database solution and not PHP.





Good luck.
Reply:I assume that your dates are stored in your database as the number of seconds after some specific time.





If not, convert the timestamp to such a format first.





Keep in mind the 86400==24*60*60 which is the number of seconds in a day.





You need to pass its result to this function:





function RoundDay($timestamp) {


return (((int) $timestamp / 86400) * 86400);


}





function RentDue($borrowed,$period=21) {


return RoundDay($borrowed+$period*86400);


}





Then, if further down you need to convert this to test the dates, you would:





if (RentDue($borrowed) %26gt; mktime()) {


overdue();


} else {


not_overdue();


}





Hope this helps.

veneers

No comments:

Post a Comment