Chinaunix首页 | 论坛 | 博客
  • 博客访问: 409337
  • 博文数量: 155
  • 博客积分: 2590
  • 博客等级: 少校
  • 技术积分: 2161
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-25 09:33
文章分类

全部博文(155)

文章存档

2015年(1)

2014年(2)

2013年(55)

2012年(97)

分类: 系统运维

2012-11-30 11:11:29

当处理一个项目的论坛,我不得不打印一个课题的时间轴格式“N秒前”或“X分钟前”。!Web 2.0的世界已悄悄改变了这些日期显示,现在的网页不仅仅是数据包装在HTML中,但他们已经变得更加真实的实时互动的人。和这样的时间戳也得到了更改为“2分钟前”、“五个小时前“像格式。这种识别更加活泼的溪流,你可以想象他们在事件发生前几分钟。
 
以下是PHP函数,可以用来转换时间轴到微博实现“X分钟前”格式。
 
/* Works out the time since the entry post, takes a an argument in unix time (seconds)
*/
static public function Timesince($original) {
    // array of time period chunks
    $chunks = array(
    array(60 * 60 * 24 * 365 , 'year'),
    array(60 * 60 * 24 * 30 , 'month'),
    array(60 * 60 * 24 * 7, 'week'),
    array(60 * 60 * 24 , 'day'),
    array(60 * 60 , 'hour'),
    array(60 , 'min'),
    array(1 , 'sec'),
    );
 
    $today = time(); /* Current unix time  */
    $since = $today - $original;
 
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
 
    $seconds = $chunks[$i][0];
    $name = $chunks[$i][1];
 
    // finding the biggest chunk (if the chunk fits, break)
    if (($count = floor($since / $seconds)) != 0) {
        break;
    }
    }
 
    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
 
    if ($i + 1 < $j) {
    // now getting the second item
    $seconds2 = $chunks[$i + 1][0];
    $name2 = $chunks[$i + 1][1];
 
    // add second item if its greater than 0
    if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
        $print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";
    }
    }
    return $print;
}
 
输入这个函数将时间轴在Unix秒。如果您的数据库表有时间轴字段可以将它转换为Unix时间轴通过使用Unix时间轴()方法在MySQL。
 
SELECT id, username, UNIX_TIMESTAMP(joined_data) from UserTable;

行业门户()文章,希望大家可以留言建议

阅读(1494) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~