php获取两个时间戳之间相隔多少天多少小时多少分多少秒
/**
* 返回两个时间的相距时间,*年*月*日*时*分*秒
* @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获取两个时间戳之间相隔多少天多少小时多少分多少秒的更多相关文章
- js 获取两个时间戳之间相隔多少天多少小时多少分多少秒
<script> function getRemainderTime (startTime){ var s1 = new Date(startTime.replace(/-/g, &quo ...
- php 获取 两个时间戳之间 相隔 【多少年】 【 多少个月】 【多少天】 【 多少个小时】 【多少分】【 多少秒 】
/** * 返回两个时间的相距时间,*年*月*日*时*分*秒 * @param int $one_time 时间戳一 大的时间戳 * @param int $two_time 时间戳二 小的时间戳 * ...
- js 计算两个时间戳之间相隔天数
var start=1491789600000;//2017-4-10 10:00 var end=1494381600000;//2017-5-10 10:00 var utc=end-start; ...
- JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)
时间戳转换为时间 // 时间戳转换为时间 function timestampToTime(timestamp, isMs = true) { const date = new Date(timest ...
- java计算两个日期之间相隔的月份(向下取整)
最近需求里面有个需要计算两个日期之间相隔的月份,写起来还挺繁琐,需要将各种情况都要考虑到,写了一个作为以后自己的工具吧. //获取哪一天 public static int getDay(Date d ...
- 如何计算CDS view里两个时间戳之间的天数间隔
ABAP透明表里的时间戳,数据类型为dec: 有个需求:计算这两个时间戳之间的天数间隔,丢弃时间戳年-月-日8位后面的小时:分钟:秒. 举个例子:如果时间戳是20180918173132,丢弃1731 ...
- Java 获取两个日期之间的日期
1.前期需求,两个日期,我们叫他startDate和endDate,然后获取到两个日期之间的日期 /** * 获取两个日期之间的日期 * @param start 开始日期 * @param end ...
- php如何计算两个时间戳之间相差的日时分秒
/功能:计算两个时间戳之间相差的日时分秒//$begin_time 开始时间戳//$end_time 结束时间戳function timediff($begin_time,$end_time){ if ...
- PHP 计算两个时间戳之间相差的时间
//功能:计算两个时间戳之间相差的日时分秒 //$begin_time 开始时间戳 //$end_time 结束时间戳 function timediff($begin_time,$end_time) ...
随机推荐
- Spring Boot程序获取tomcat启动端口
package com.geostar.geostack.git_branch_manager.config; import org.springframework.beans.factory.ann ...
- Go语言公开或未公开的标识符
Go语言公开或未公开的标识符的基本概念 Go语言支持从包里公开或者隐藏标志符,通过这个特性,可以让用户按照自己的规则控制标识符的可见性. Go语言中的可见性,是通过声明类型的大小写来进行区别的. 例如 ...
- Pthread 用法笔记
什么是线程? 从技术上讲,一个线程被定义为一个独立的指令流. 一个进程可以包含一个或多个线程. 线程操作包括线程创建,终止,同步(连接,阻塞),调度,数据管理和进程交互. 进程内的所有线程共享: 相同 ...
- GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件
GroupBox:划分窗体区域,内部可以拖放组件 TextBox:可编辑文本框,也可设置为只读 属性:ReadOnly(只读).PasswordChar(密码显示的符号,如*).Multiline(多 ...
- kubernetes云平台管理实战: pod资源共享(三)
一.共享容器IP地址 1.查看容器进程 [root@k8s-node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS ...
- Java基础知识拾遗(一)
类型提升规则 Java定义了几个应用于表达式的类型提升规则:所有byte.short和char类型的值都被提升为int类型.如果有一个操作数是long类型,将这个表达式提升为 long 类型:如果有一 ...
- Regularity criteria for NSE 4: $\p_3u$
In [Zhang, Zujin. An improved regularity criterion for the Navier–Stokes equations in terms of one d ...
- EffectiveC++ 第7章 模板与泛型编程
我根据自己的理解,对原文的精华部分进行了提炼,并在一些难以理解的地方加上了自己的"可能比较准确"的「翻译」. Chapter 7 模版与泛型编程 Templates and Gen ...
- 高并发秒杀系统--Service事务管理与继承测试
[Spring IoC的类型及应用场景] [Spring事务使用方式] [Spring事务的特性] [Spring事务回滚的理解] [Service声明式事务的配置] 1.配置事务管理器 2.配置基 ...
- css Modules 使用
我目前常用的也就是引用类名,以及在需要修改某个ui组件原有的样式比较麻烦时,会使用 :global className{ color: red;}这样来修改... 更多请参考阮老师博客: http:/ ...