Home > Backend Development > PHP Problem > How to calculate the difference between time and how many days in php

How to calculate the difference between time and how many days in php

青灯夜游
Release: 2023-03-15 14:02:01
Original
5030 people have browsed it

php method to calculate the difference in days between time subtraction: 1. Use the "strtotime("time")" statement to convert the two times into timestamps; 2. Subtract the two timestamps to get the time difference ;3. Divide the time difference by the total number of seconds in a day and use floor() to round. The syntax is "floor(time difference/86400)".

How to calculate the difference between time and how many days in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

How to calculate time with php Subtract the difference in days

Implementation idea:

  • Use strtotime() function to convert two times into timestamp

  • Subtract the two timestamps to get the time difference

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

  • Use floor() to round

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
//2022年1月1日 19点30分0秒
$time1=strtotime("2022-1-1");
//2022年7月7日 7点30分0秒
$time2=strtotime("2022-7-7");

$diff_seconds = $time2 - $time1;
$diff_days = floor($diff_seconds/86400);
echo "两个时间相差: ".$diff_days." 天";
?>
Copy after login

How to calculate the difference between time and how many days in php

Recommended study: " PHP video tutorial

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

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