PHP method to calculate the difference between two times_PHP tutorial

WBOY
Release: 2016-07-13 17:40:22
Original
1098 people have browsed it

Calculating time differences in PHP is sometimes troublesome! But as long as you master the usage of date and time functions, it will become simple:
A simple example is to calculate the number of days to borrow a book, which requires PHP to calculate the time difference based on To calculate the date of each day, let’s talk about several methods to implement this kind of date calculation:
(1) It’s very easy if you have a database! If you have MSSQL, you can use triggers! Use the function datediff that specializes in calculating date differences. () will do!
If it is MYSQL, then use the difference between the two date fields to calculate the result and save it in another numeric field! Just call it when needed!

(2) If there is no database , then you have to completely 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 easy to compile More:
$days=round(($enddate-$startdate)/3600/24) ;
echo $days;
//days is the number of days obtained;
If in mktime() The parameter is defaulted, which means using the current date, so that the number of days from the date of borrowing the book can be calculated.
Don’t forget to return the book when it is due! There will be a fine for overdue books!!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486220.htmlTechArticleCalculating time difference in php is sometimes troublesome! But as long as you master the usage of date and time functions, then These become simple: a simple example is to calculate the number of days to borrow a book...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template