A simple example is to calculate the number of days to borrow a book, which requires PHP to calculate based on the date of each day. Let’s talk about several methods to implement this date calculation:
(1) It is very easy if you have a database! If it is MSSQL, you can use triggers! Just use the function datediff() that specially calculates the date difference!
If it is MYSQL, use the difference between the two date fields to calculate the result and save it in another numeric field! Call it when needed That’s it!
(2) If there is no database, you have to use PHP’s time and date function! The following is the main explanation:
Example: Calculate the number of days from May 3, 1998 to 1999-6-5:
$startdate=mktime("0","0","0","5","3","1998");
$enddate=mktime("0 ","0","0","6","5","1999");
//The obtained value is the total number of seconds from 1970-1-1 to the parameter time: it is an integer. Then
//The following code is much easier to compile:
$days=round(($enddate-$startdate)/3600/24);
echo $days;
/ /days is the number of days obtained;
?>
If the parameter in mktime() is defaulted, it means using the current date, so that the number of days from the date of borrowing the book can be calculated.