Home > Backend Development > PHP Tutorial > PHP function to calculate the time between two timestamps (hours)_PHP tutorial

PHP function to calculate the time between two timestamps (hours)_PHP tutorial

WBOY
Release: 2016-07-21 15:41:50
Original
1135 people have browsed it

This is php code that can be specific to the hour

Copy the code The code is as follows:

/* Author : Yang Yu yangyu@sina.cn */
//Enter two timestamps and calculate the difference, which is the number of hours. If 2:10 is returned, it means that the difference between the two entered times is 2 hours and 10 minutes.
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;
}

The following is the function code js that is specific to the number of days
Copy the code 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('content') .innerHTML = 'The difference between the two dates is'+time+' days! ';}

Date difference online calculation tool

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321113.htmlTechArticleThis is a php code that can be specific to the hour. Copy the code as follows: /* Author: Yang Yu yangyu@sina. cn */ //Enter two timestamps and calculate the difference, which is the number of hours difference, if returned...
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