Two ways to calculate the difference between two timestamps in PHP

小云云
Release: 2023-03-17 15:10:02
Original
7963 people have browsed it

Timestamp (timestamp), usually a sequence of characters, uniquely identifies a certain moment in time. Digital timestamp technology is a variant of digital signature technology. Time is also a very important requirement in PHP. In this article, we mainly talk about two methods for PHP to calculate the day, hour, minute and second difference between two timestamps.

Recommended manual: php complete self-study manual

First type:

/** * 计算两个时间戳之间相差的日时分秒 * @param $unixTime_1 开始时间戳 * @param $unixTime_2 结束时间戳 * @return array */ function timeDiff($unixTime_1, $unixTime_2) { $timediff = abs($unixTime_2 - $unixTime_1); //计算天数 $days = intval($timediff / 86400); //计算小时数 $remain = $timediff % 86400; $hours = intval($remain / 3600); //计算分钟数 $remain = $remain % 3600; $mins = intval($remain / 60); //计算秒数 $secs = $remain % 60; return ['day' => $days, 'hour' => $hours, 'min' => $mins, 'sec' => $secs]; } //Array ( // [day] => 1 // [hour] => 0 // [min] => 3 // [sec] => 31 //)
Copy after login
Related article recommendations:
1. php about date and time operations
Related video recommendations:
1. Dugu Jiujian (4)_PHP video tutorial

Second type:

/** * 计算两个时间戳之间相差的日时分秒 * @param $date1 开始时间 * @param $date2 结束时间 * @return array */ function diffDate($date1, $date2) { $datetime1 = new \DateTime($date1); $datetime2 = new \DateTime($date2); $interval = $datetime1->diff($datetime2); $time['year'] = $interval->format('%Y'); $time['month'] = $interval->format('%m'); $time['day'] = $interval->format('%d'); $time['hour'] = $interval->format('%H'); $time['min'] = $interval->format('%i'); $time['sec'] = $interval->format('%s'); $time['days'] = $interval->format('%a'); // 两个时间相差总天数 return $time; } //Array ( // [year] => 00 // [month] => 1 // [day] => 2 // [hour] => 00 // [min] => 3 // [sec] => 31 // [days] => 33 //)
Copy after login

The above are two ways for PHP to calculate the day, hour, minute and second difference between two timestamps. Method, I hope it can help everyone.

Related recommendations:

php timestamp

Timestamp PHP timestamp usage example code

Summary of usage of php timestamp function

Example explanation of PHP timestamp_PHP tutorial

The maximum time that php timestamp can express What is it and how to solve it

The above is the detailed content of Two ways to calculate the difference between two timestamps 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!