Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 计算两个时间戳相隔的时间的函数(php与js版)

计算两个时间戳相隔的时间的函数(php与js版)

发布时间:2015-07-07   编辑:www.jquerycn.cn
计算两个时间戳相隔的时间,一个具体到小时,一个具体到天数,有需要的朋友可以参考下。

计算两个时间戳相隔的时间,一个具体到小时,一个具体到天数,有需要的朋友可以参考下。

1、具体到小时的php代码
 

复制代码 代码如下:
<?php
/* Author: 杨宇 yangyu@sina.cn */
//输入两个时间戳,计算差值,也就是相差的小时数,如返回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;
}

2、具体到天数的js代码
 

复制代码 代码如下:
<script language="javascript">
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 = '两个日期相差 <strong style="color:red">'+time+'</strong> 天!';}
</script>

您可能感兴趣的文章:
计算两个时间戳相隔的时间的函数(php与js版)
php 获取今日、昨日、上周、本月的起始与结束时间戳
php获取本周、本月第一天与最后一天的时间戳
Javascript时间戳与php时间戳转换时要注意什么
php如何计算两个时间戳之间相差的日时分秒
php字符串转时间戳
php 创建以unix时间戳命名的文件夹
php时间函数strtotime的深入理解
php日期与时间运算实例分享
php日期函数的简单示例代码

[关闭]