Home > Backend Development > PHP Tutorial > PHP calculates the time interval between two timestamps

PHP calculates the time interval between two timestamps

PHP中文网
Release: 2023-03-13 14:34:01
Original
2264 people have browsed it

Timestamp (timestamp), a complete and verifiable data that can indicate that a piece of data existed before a specific time, usually a sequence of characters that uniquely identifies a certain moment time. Using data generated by digital signature technology, the object of the signature includes original file information, signature parameters, signature time and other information. It is widely used in intellectual property protection, contract signing, financial accounting, electronic quotation and bidding, stock trading, etc.

This article mainly introduces how to use php to calculate the time between two timestamps. The code is as follows

This can be specific to the hour

//输入两个时间戳,计算差值,也就是相差的小时数,如返回2:10,则表示输入的两个时间相差2小时10分钟 
function hours_min($start_time,$end_time){ 
if (strtotime($start_time) > strtotime($end_time)) list($start_time, $end_time) = array($end_time, $start_time); 
$sec = $start_time - $end_time; 
$sec = round($sec/60); 
$min = str_pad($sec%60, 2, 0, STR_PAD_LEFT); 
$hours_min = floor($sec/60); 
$min != 0 && $hours_min .= ':'.$min; 
return $hours_min; 
}
Copy after login

The following is the function code js specific to the number of days

The code is as follows:

function get_date_different(){ 
var _date_1 = document.getElementById('date1').value.replace(/(^\s*)|(\s*$)/g,''); 
var _date_2 = document.getElementById('date2').value.replace(/(^\s*)|(\s*$)/g,''); 
_date_1 = new Date(_date_1); 
_date_2 = new Date(_date_2); 
var days= _date_2.getTime() - _date_1.getTime(); 
var time = parseInt(days / (1000 * 60 * 60 * 24)); 
document.getElementById(&#39;content&#39;).innerHTML = &#39;两个日期相差 <strong style="color:red">&#39;+time+&#39;</strong> 天!&#39;;}</script>
Copy after login

The above is the detailed content of PHP calculates the time interval between two timestamps. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template