博客
关于我
PHP 计算时间差
阅读量:121 次
发布时间:2019-02-26

本文共 1287 字,大约阅读时间需要 4 分钟。

<?php

/**
时间差计算
*
@param Timestamp $time
*
@return String Time Elapsed
*
@author Shelley Shyan
*
@copyright http://phparch.cn (Professional PHP Architecture)
*/
functiontime2Units($time){
$year = floor($time / 60 / 60 / 24 / 365); $time -= $year * 60 * 60 * 24 * 365; $month = floor($time / 60 / 60 / 24 / 30); $time -= $month * 60 * 60 * 24 * 30; $week = floor($time / 60 / 60 / 24 / 7); $time -= $week * 60 * 60 * 24 * 7; $day = floor($time / 60 / 60 / 24); $time -= $day * 60 * 60 * 24; $hour = floor($time / 60 / 60); $time -= $hour * 60 * 60; $minute = floor($time / 60); $time -= $minute * 60; $second = $time; $elapse = ''; // 定义时间单位数组 $unitArr = array( '年' => 'year', '个月' => 'month', '周' => 'week', '天' => 'day', '小时' => 'hour', '分钟' => 'minute', '秒' => 'second' ); // 循环计算各时间单位并累加 foreach ($unitArr as $cn => $u) { if ($u > 0) { $elapse .= "$u $cn "; break; } } return $elapse;}

$past = 2052345678;// 某个过去的时间戳$now = time();// 当前时间戳$diff = $now - $past;$diff = abs($diff);

发表于 time2Units($diff) 前

转载地址:http://sssf.baihongyu.com/

你可能感兴趣的文章
php与web服务器关系
查看>>
redis事务操作
查看>>
php中0,空,null和false的区别
查看>>
PHP中array_merge和array相加的区别分析
查看>>
PHP中Closure::bindTo的用法分析
查看>>
php中curl得使用
查看>>
PHP中curl特性
查看>>
PHP中date时间不对
查看>>
PHP中dirname(__FILE__)的意思
查看>>
PHP中extract()函数的妙用
查看>>
PHP中fileinfo的作用以及怎么开启fileinfo
查看>>
PHP中file_get_contents如何带上cookies
查看>>
PHP中header的作用
查看>>
PHP中implode()和explode()
查看>>
PHP中ob系列函数讲解(浏览器缓存技术)
查看>>
PHP中serialize和json序列化与反序列化的区别
查看>>
Redis事务处理
查看>>
php中传值与传引用的区别是什么
查看>>
php中使用ajax进行前后端json数据交互
查看>>
Redis事务和锁操作
查看>>