Home  >  Article  >  Backend Development  >  How to calculate time difference in php_PHP tutorial

How to calculate time difference in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:36849browse

How to calculate the time difference in PHP

Several methods of calculating the time difference in PHP. Calculating the time difference in PHP is sometimes a troublesome thing! But as long as you master the date and time The usage of functions becomes simple:

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 achieve this date calculation:

(1) If you have a database, it will be easy! If you have MSSQL, you can use triggers! Just use the function datediff(), which is specially used to calculate date differences!

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, 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:

$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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847197.htmlTechArticleHow to calculate the time difference in php. There are several ways to calculate the time difference in PHP. Calculating the time difference in php is sometimes a piece of cake. Troublesome! But as long as you master the usage of date and time functions, then these...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn