<?php
header("Content-type: text/html; charset=utf-8");
function time2Units ($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 = strtotime("2015-01-12 21:49:00"); // Some timestamp in the past
$now = time(); // Current timestamp
$diff = $now - $past;
echo '发表于' . time2Units($diff) . '前';

?>

PHP计算时间差,并返回什么时间之前发表的内容的更多相关文章

  1. java计算时间差 Java问题通用解决代码

    java实现计算时间差     正式版:       /**        * 计算时间差,求出两者相隔的时间        *        * @param nowDate        *    ...

  2. Oracle计算时间差

    Oracle计算时间差表达式 --获取两时间的相差豪秒数 select ceil((To_date('2008-05-02 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - ...

  3. 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结

    一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...

  4. 关于js中的时间——计算时间差等

    获取当前(系统)时间: var NowDate= new Date(); // 获取当前日期时间 // 输出为: Wed May 03 2017 14:52:08 GMT+0800 (中国标准时间) ...

  5. golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差

    获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...

  6. c/c++在windows下获取时间和计算时间差的几种方法总结 【转】

    http://blog.csdn.net/coder_xia/article/details/6566708 一.标准C和C++都可用 1.获取时间用time_t time( time_t * tim ...

  7. c和c++在windows下获取时间和计算时间差的方法总结

    c/c++在windows下获取时间和计算时间差的几种方法总结 一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double diff ...

  8. DB2--sql计算时间差和格式化时间

    格式化时间 db2 格式化时间使用的 TO_CHAR(TIMESTAMP('2017-10-24 21:18:12'),'YYYY-MM-DD'): 测试sql:SELECT TO_CHAR(TIME ...

  9. js使用my97插件显示当前时间,且select控制计算时间差

    做页面需要两个时间输入框一个显示当前时间,一个显示之前的时间,并且需要一个select下拉框控制两个时间输入框之间的差,效果如下图: 这里使用的是My97DatePicer,简单方便,引入my97插件 ...

随机推荐

  1. $.getJSON JSONP的新坑

    神坑1:返回的内容必须是正规的json数据.如 { "firstName": "Bill", "lastName": "Gates ...

  2. 2145334赵文豪《Java程序设计》第2周学习总结

    2145334赵文豪<Java程序设计>第2周学习总结 教材学习内容总结 第二周的学习结束了,又是充实的一周,在这周的java学习过程中,我们主要学习了java的基础语法.其中包括类型变量 ...

  3. Android课程---Activity 的生命周期

    activity类处于android.app包中,继承体系如下: 1.java.lang.Object 2.android.content.Context 3.android.app.Applicat ...

  4. IOS第四天(1:图片的方法和缩小,遮罩层)

    @interface HMViewController () @property (nonatomic, strong) UIButton *cover; //阴影 @end @implementat ...

  5. PHP的排序函数的总结

    Sort     破坏索引 升序    值排序 Rsort    破坏索引 降序    值排序 Asort    保持索引 升序     值排序 Arsort   保持索引 降序     值排序 Ks ...

  6. Postgres-enum

    -- 1. rename the enum type you want to change alter type some_enum_type rename to _some_enum_type; - ...

  7. log4j.properties 配置的学习整理

    参考资料: log4j.properties:用来做什么的(日志) Log4j:由2部分组成 :loggers(记录器)            ----日志的类别 appender(输出源)     ...

  8. tomcat session cluster

    Session的生命周期 以前在学习的时候没怎么注意,今天又回过头来仔细研究研究了一下Session的生命周期. Session存储在服务器端,一般为了防止在服务器的内存中(为了高速存取),Sessi ...

  9. 【五子棋AI循序渐进】——开局库

    首先,对前面几篇当中未修复的BUG致歉,在使用代码时请万分小心…………尤其是前面关于VCF\VCT的一些代码和思考,有一些错误.虽然现在基本都修正了,但是我的程序还没有经过非常大量的对局,在这之前,不 ...

  10. Git diff 常见用法

      Git diff 用于比较两次修改的差异 1.1 比较工作区与暂存区 git diff  比较的是单个仓库的工作区与暂存区的差别,repo diff是对git diff的封装,用来分别显示各个项目 ...