/**
* 返回两个时间的相距时间,*年*月*日*时*分*秒
* @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((int)$one_time) && is_int((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 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)

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

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

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

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

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

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

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

  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. [再寄小读者之数学篇](2014-07-27 $H^{-1}$ 中的有界集与弱收敛极限)

    设 $H^{-1}$ 是 $H^1_0$ 的对偶空间, 定义域为 $[0,1]$. 试证: (1) $\sed{h\sin (2\pi hx);\ h>0}$ 在 $H^{-1}$ 中有界; ( ...

  2. [再寄小读者之数学篇](2014-06-20 求极限-L'Hospital 法则的应用)

    设 $f\in C[0,+\infty)$, $a$ 为实数, 且存在有限极限 $$\bex \vlm{x}\sez{f(x)+a\int_0^x f(t)\rd t}. \eex$$ 证明; $f( ...

  3. tomcat 优化建议

    下面给出的是tomcat的优化建议,如果不同意见请留言. 上配置: tomcat jmx配置访问:修改catalina.sh CATALINA_OPTS="$CATALINA_OPTS -D ...

  4. 【原创】大数据基础之Zookeeper(2)源代码解析

    核心枚举 public enum ServerState { LOOKING, FOLLOWING, LEADING, OBSERVING; } zookeeper服务器状态:刚启动LOOKING,f ...

  5. avalonjs学习笔记之实现一个简单的查询页

    官网地址:http://avalonjs.coding.me/ 因为是为了学习js,所以对样式没什么要求,先放效果图: 步骤为:初始页面-------条件查询-------编辑员工1-------保存 ...

  6. liunx 下WebBench 安装与压力测试

    安装: wget http://blog.zyan.cc/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd ...

  7. C#+EntityFramework编程方式详细之Code First

    Code First Code First模式即“代码优先”模式,是从EF4.1开始新建加入的功能.使用Code First模式进行EF开发时只需要编写对应的数据类,然后自动生成数据库. Code F ...

  8. C语言对字符串去重

    # include <stdio.h> # include <string.h> char * getNewChar(char * str,char * newStr); in ...

  9. 基于netty的socket服务端触发了channelInactive方法,但实际连接没有断开的问题

    背景: 一个中小型H5游戏,后端使用基于 netty 的socket服务 服务端 分为 分发服务器 & 业务服务器,业务服务器可负载 用户客户端与分发服务器连接 分发服务器再作为客户端与每台业 ...

  10. table切换jquery插件 jQuery插件写法模板 流程

    通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget()应用jQuery UI的部件工厂方式创建 通过$.extend()来扩展jQuery $. ...