Home > Backend Development > PHP Tutorial > How to calculate the time difference between two times in php?

How to calculate the time difference between two times in php?

怪我咯
Release: 2023-03-13 10:02:01
Original
11395 people have browsed it

This article mainly introduces the method of PHP to simply calculate the difference between two times, and analyzes the conversion of PHP date and time and related operation skills of mathematical operations in the form of specific examples. Friends in need can refer to it.

Recommended manual:php complete self-study manual

The example in this article describes the simple method of calculating the difference between two times in PHP. Share it with everyone for your reference, the details are as follows:

<?php
//PHP计算两个时间差的方法 
$startdate="2010-12-11 11:40:00";
$enddate="2012-12-12 11:45:09";
$date=floor((strtotime($enddate)-strtotime($startdate))/86400);
$hour=floor((strtotime($enddate)-strtotime($startdate))/86400/3600);
$minute=floor((strtotime($enddate)-strtotime($startdate))/86400/60);
$second=floor((strtotime($enddate)-strtotime($startdate))/86400/60);
echo $date."天<br>";
echo $hour."小时<br>";
echo $minute."分钟<br>";
echo $second."秒<br>";
Copy after login

Running results:

732天
0小时
12分钟
12秒
Copy after login
Recommended related articles:
1.php about date and time operations
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of How to calculate the time difference between two times in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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