/**
* 返回两个时间的相距时间,*年*月*日*时*分*秒
* @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. [物理学与PDEs]第1章第7节 媒质中的 Maxwell 方程组 7.3 媒质中电磁场量的表示

    1. 电磁能量密度 $$\bex \cfrac{1}{2}({\bf E}\cdot{\bf D}+{\bf B}\cdot{\bf H}). \eex$$ 2. 电磁能量流密度向量 $$\bex { ...

  2. scala使用slick查询的全过程(使用cass class)

    1. 首先导包 <dependency> <groupId>com.typesafe.slick</groupId> <artifactId>slick ...

  3. MVC5 Entity Framework学习

    MVC5 Entity Framework学习(1):创建Entity Framework数据模型 MVC5 Entity Framework学习(2):实现基本的CRUD功能 MVC5 Entity ...

  4. L1-Day3

    L1-Day31.太阳从西边落下. [我的翻译]The sun falls in the west. [标准答案]The sun sets in the west. [对比分析]落下fall与set的 ...

  5. JDK8- interface接口可以写方法体了-- 惊 dt.jar包等

    基本上所有的Java教程都会告诉我们Java接口的方法都是public.abstract类型的,没有方法体的. 但是在JDK8里面,你是可以突破这个界限的哦. == 一个类实现某个接口,必须重写接口中 ...

  6. Git可视化教程——Git Gui的使用

    参考链接:https://blog.csdn.net/qq_34842671/article/details/70916587

  7. tmux 后台训练

    参考链接:https://blog.csdn.net/u014381600/article/details/54588531

  8. vCenter Server 6 Standard

    准备环境和工具: 三台 ESXi 6.0主机: 准备一台Windows Server 2008 R2系统的虚拟机: VMware-VIM-all-6.0.0.iso 软件下载地址 链接: https: ...

  9. 【原创】大叔经验分享(46)用户提交任务到yarn报错

    用户提交任务到yarn时有可能遇到下面的错误: 1) Requested user anything is not whitelisted and has id 980,which is below ...

  10. 校验XX是否在有效期内

    简单介绍:做删除的时候,需要判断XX的日期,如果在有效期内,则不能删除,已过期,或者是未生效都可以删除.刚看到的时候,就有点懵逼了,因为不知道该怎么判断,最初,想的是查询XX的开始日期和截止日期,看看 ...