A simple example is to calculate the number of days to borrow a book. This 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() which is specially used to calculate the date difference!
If it is MYSQL, then use the difference between the two date fields to calculate the calculation result and save it in another numeric field! Just call it when needed!
(2) If there is no database, then 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 parameters in mktime() are default, it means the current date is used , so that the number of days from the borrowing date to the present can be calculated.
The above introduces the 2014 Zhejiang Provincial Civil Service Examination registration time. Several methods for calculating the time difference in PHP, including the 2014 Zhejiang Provincial Civil Service Examination registration time. I hope it will be helpful to friends who are interested in PHP tutorials.