博客
关于我
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:$_ENV 和 getenv区别
查看>>
PHP:PDOStatement::bindValue参数类型php5和php7问题
查看>>
pickle模块
查看>>
pid控制
查看>>
PID控制介绍-ChatGPT4o作答
查看>>
PID控制器数字化
查看>>
PIESDKDoNet二次开发配置注意事项
查看>>
PIGS POJ 1149 网络流
查看>>
PIL Image对图像进行点乘,加上常数(等像素操作)
查看>>
PIL.Image、cv2的img、bytes相互转换
查看>>
PIL.Image进行图像融合显示(Image.blend)
查看>>
Pillow lacks the JPEG 2000 plugin
查看>>
ping 全网段CMD命令
查看>>
ping 命令的七种用法,看完瞬间成大神
查看>>
Pinia:$patch的使用场景
查看>>
Pinia:$subscribe()的使用场景
查看>>
Pinpoint对Kubernetes关键业务模块进行全链路监控
查看>>
Pinterest 大规模缓存集群的架构剖析
查看>>
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>
PinYin4j库的使用
查看>>