/**
* 返回两个时间的相距时间,*年*月*日*时*分*秒
* @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;
}

最新文章

  1. 一次更愚蠢的NOIP模拟赛
  2. WPF:设置弹出子菜单的是否可用状态及效果
  3. 转:电子取证中AVI文件的文件雕复
  4. windows 8 系统部署IIS并发布网站
  5. 实战SQL Server 2005镜像配置全过程
  6. [C语言 - 10] C语言保留字
  7. UPC 2224 Boring Counting ★(山东省第四届ACM程序设计竞赛 tag:线段树)
  8. eclipse引用头文件报错问题-解决方法
  9. 【Linux安全】chattr命令锁定账户敏感文件
  10. asp.neti 加密三种方式
  11. apt-get 总结2
  12. timestamp时间戳的应用(微信小程序开发也一样)
  13. java多线程中注入Spring对象问题
  14. adduser Ubuntu添加sudo用户
  15. LaF: Fast Access to Large ASCII Files
  16. MySql存储过程与函数详解
  17. python流程控制while和if
  18. IOS初级:UIAlertController
  19. poj1573模拟
  20. iPhone激活策略说明

热门文章

  1. VIM --使用进阶 -- 插件篇 -- YouCompleteMe -- nerdtree
  2. 邮箱学堂:SPF详解
  3. java处理含有中文的字符串.
  4. springboot项目中如何在pom文件覆盖starter中默认指定的jar版本号
  5. installshield安装包制作
  6. fiddler抓包工具
  7. Scrapyd
  8. intellij idea创建maven项目
  9. ubuntu+github配置使用
  10. vue入门知识点