Several ways to calculate time differences in PHP

  • 2020-03-31 20:18:15
  • OfStack

A simple example is to calculate the number of days a book is borrowed, which requires PHP to calculate based on the date of each day. Here are a few ways to do this:
(1) it would be easy if you had a database! If MSSQL can use triggers! Use datediff(), a function that calculates date differences!
If it is MYSQL, then use the difference between the two date fields to calculate the results of the calculation saved in another numeric field! When called!
(2) if you don't have a database, you'll have to use PHP's time and date function entirely! The following is the main explanation:
Example: the number of days from May 3, 1998 to June 5, 1998:
< ? PHP
$startdate = mktime (" 0 "and" 0 "and" 0 ", "5", "3", "1998");
$enddate = mktime (" 0 "and" 0 "and" 0 ", and "6", "5", "1999");
// the value obtained is the total number of seconds from 1970-1-1 to the parameter time: is an integer

// the following code is much easier to code:
$days = round (($enddate - $startdate) / 3600/24);
Echo $days;
//days is the number of days obtained;
? >
If the parameter in mktime() defaults, the current date is used, and the number of days since the loan date is calculated.

Related articles: