How to calculate the difference between two timestamps in php

青灯夜游
Release: 2023-03-15 11:58:01
Original
5563 people have browsed it

php method to calculate how many days the difference between two timestamps is: 1. Subtract the two timestamps to get the time difference; 2. Divide the time difference by the total number of seconds in a day "86400", and use floor( ) function, syntax "floor(time difference/86400)".

How to calculate the difference between two timestamps in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php calculates two timestamps How many days apart

Implementation idea:

  • Subtract the two timestamps to get the time difference

  • The time difference is divided by the total number of seconds in a day (24*60*60=86400)

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
ini_set(&#39;date.timezone&#39;, &#39;Asia/Shanghai&#39;);
//2021年1月1日19点30分0秒
$time1="1609500600";
//2021年7月7日7点30分0秒
$time2="1625614200";
$diff_seconds = $time2 - $time1;
$diff_days = floor($diff_seconds/86400);
echo "两个时间戳相差: ".$diff_days." 天";
?>
Copy after login

How to calculate the difference between two timestamps in php

Recommended learning: "PHP video tutorial"

The above is the detailed content of How to calculate the difference between two timestamps 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