/**
* 返回两个时间的相距时间,*年*月*日*时*分*秒
* @param int $one_time 时间一
* @param int $two_time 时间二
* @param int $return_type 默认值为0,0/不为0则拼接返回,1/*秒,2/*分*秒,3/*时*分*秒/,4/*日*时*分*秒,5/*月*日*时*分*秒,6/*年*月*日*时*分*秒
* @param array $format_array 格式化字符,例,array('年', '月', '日', '时', '分', '秒')
* @return String or false
*/
function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '时', '分', '秒'))
{
if ($return_type < 0 || $return_type > 6) {
return false;
}
if (!(is_int($one_time) && is_int($two_time))) {
return false;
}
$remainder_seconds = abs($one_time - $two_time);
//年
$years = 0;
if (($return_type == 0 || $return_type == 6) && $remainder_seconds - 31536000 > 0) {
$years = floor($remainder_seconds / (31536000));
}
//月
$monthes = 0;
if (($return_type == 0 || $return_type >= 5) && $remainder_seconds - $years * 31536000 - 2592000 > 0) {
$monthes = floor(($remainder_seconds - $years * 31536000) / (2592000));
}
//日
$days = 0;
if (($return_type == 0 || $return_type >= 4) && $remainder_seconds - $years * 31536000 - $monthes * 2592000 - 86400 > 0) {
$days = floor(($remainder_seconds - $years * 31536000 - $monthes * 2592000) / (86400));
}
//时
$hours = 0;
if (($return_type == 0 || $return_type >= 3) && $remainder_seconds - $years * 31536000 - $monthes * 2592000 - $days * 86400 - 3600 > 0) {
$hours = floor(($remainder_seconds - $years * 31536000 - $monthes * 2592000 - $days * 86400) / 3600);
}
//分
$minutes = 0;
if (($return_type == 0 || $return_type >= 2) && $remainder_seconds - $years * 31536000 - $monthes * 2592000 - $days * 86400 - $hours * 3600 - 60 > 0) {
$minutes = floor(($remainder_seconds - $years * 31536000 - $monthes * 2592000 - $days * 86400 - $hours * 3600) / 60);
}
//秒
$seconds = $remainder_seconds - $years * 31536000 - $monthes * 2592000 - $days * 86400 - $hours * 3600 - $minutes * 60;
$return = false;
switch ($return_type) {
case 0:
if ($years > 0) {
$return = $years . $format_array[0] . $monthes . $format_array[1] . $days . $format_array[2] . $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
} else if ($monthes > 0) {
$return = $monthes . $format_array[1] . $days . $format_array[2] . $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
} else if ($days > 0) {
$return = $days . $format_array[2] . $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
} else if ($hours > 0) {
$return = $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
} else if ($minutes > 0) {
$return = $minutes . $format_array[4] . $seconds . $format_array[5];
} else {
$return = $seconds . $format_array[5];
}
break;
case 1:
$return = $seconds . $format_array[5];
break;
case 2:
$return = $minutes . $format_array[4] . $seconds . $format_array[5];
break;
case 3:
$return = $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
break;
case 4:
$return = $days . $format_array[2] . $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
break;
case 5:
$return = $monthes . $format_array[1] . $days . $format_array[2] . $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
break;
case 6:
$return = $years . $format_array[0] . $monthes . $format_array[1] . $days . $format_array[2] . $hours . $format_array[3] . $minutes . $format_array[4] . $seconds . $format_array[5];
break;
default:
$return = false;
}
return $return;
}

  

php获取两个时间戳之间相隔多少天多少小时多少分多少秒的更多相关文章

  1. js 获取两个时间戳之间相隔多少天多少小时多少分多少秒

    <script> function getRemainderTime (startTime){ var s1 = new Date(startTime.replace(/-/g, &quo ...

  2. php 获取 两个时间戳之间 相隔 【多少年】 【 多少个月】 【多少天】 【 多少个小时】 【多少分】【 多少秒 】

    /** * 返回两个时间的相距时间,*年*月*日*时*分*秒 * @param int $one_time 时间戳一 大的时间戳 * @param int $two_time 时间戳二 小的时间戳 * ...

  3. js 计算两个时间戳之间相隔天数

    var start=1491789600000;//2017-4-10 10:00 var end=1494381600000;//2017-5-10 10:00 var utc=end-start; ...

  4. JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)

    时间戳转换为时间 // 时间戳转换为时间 function timestampToTime(timestamp, isMs = true) { const date = new Date(timest ...

  5. java计算两个日期之间相隔的月份(向下取整)

    最近需求里面有个需要计算两个日期之间相隔的月份,写起来还挺繁琐,需要将各种情况都要考虑到,写了一个作为以后自己的工具吧. //获取哪一天 public static int getDay(Date d ...

  6. 如何计算CDS view里两个时间戳之间的天数间隔

    ABAP透明表里的时间戳,数据类型为dec: 有个需求:计算这两个时间戳之间的天数间隔,丢弃时间戳年-月-日8位后面的小时:分钟:秒. 举个例子:如果时间戳是20180918173132,丢弃1731 ...

  7. Java 获取两个日期之间的日期

    1.前期需求,两个日期,我们叫他startDate和endDate,然后获取到两个日期之间的日期 /** * 获取两个日期之间的日期 * @param start 开始日期 * @param end ...

  8. php如何计算两个时间戳之间相差的日时分秒

    /功能:计算两个时间戳之间相差的日时分秒//$begin_time 开始时间戳//$end_time 结束时间戳function timediff($begin_time,$end_time){ if ...

  9. PHP 计算两个时间戳之间相差的时间

    //功能:计算两个时间戳之间相差的日时分秒 //$begin_time 开始时间戳 //$end_time 结束时间戳 function timediff($begin_time,$end_time) ...

随机推荐

  1. [BZOJ 3110] [ZJOI 2013] K大数查询

    Description 有 \(N\) 个位置,\(M\) 个操作.操作有两种,每次操作如果是: 1 a b c:表示在第 \(a\) 个位置到第 \(b\) 个位置,每个位置加入一个数 \(c\): ...

  2. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  3. pgsql 并行相关配置

  4. ADO.NET中的五大内置对象

    ADO.NET中的五大内置对象 学习链接:https://blog.csdn.net/wxr15732623310/article/details/51828677

  5. js获取url参数(通用方法)

    function getUrl(name="") { var url = location.search; //获取url中"?"符后的字串 var theRe ...

  6. JavaFile、递归、字节流、字符流整理

    File 1.1                File类的构造函数 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读取到内存中的这个动作称为输入 ...

  7. IDEA配置注释模板

    直接进入主题: Ctrl+Alt+S进入设置界面(我没改过按键映射,你也可以从File-OtherSetting进入设置),找到Editor->File and Code Templates,先 ...

  8. 解决Ubuntu 17.10设置面板打不开的问题

    问题描述 对于Ubuntu桌面系统我用得不多,最近安装了Ubuntu17.10使用,一直都没遇到什么大的问题,界面风格已经与Windows很相似,总体体验还不错.直到某一天我突然手痒痒把Dock面板从 ...

  9. [物理学与PDEs]第1章第2节 预备知识 2.2 Ampere-Biot-Savart 定律, 静磁场的散度与旋度

    1. 电流密度, 电荷守恒定律 (1) 电荷的定向移动形成电流. (2) 电流密度 ${\bf j}$, 是描述导体内一点在某一时刻电流流动情况的物理量, 用单位时间内通过垂直于电流方向的单位面积的电 ...

  10. How far away ? HDU - 2586 【LCA】【RMQ】【java】

    题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最 ...