收藏一个老外写的,专门用来显示比如“2 hours ago,1 hour ago"之类时间的函数,
请注意其中对英文单复数时间的处理:
<?php
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类型的时间,如果数据库中传入的是TIMESTAMP类型,可以转换下
SELECT id, username, UNIX_TIMESTAMP(joined_data) from UserTable;
一个显示多少时间之前的函数
原创
©著作权归作者所有:来自51CTO博客作者mb5c80f4c73b73a的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
币是多少时间上涨的
比特币是多少时间上涨的
比特币 技术发展 历史数据 -
js多少时间之前
Document
js html php css jquery -
java Stable悬浮窗
write once ,run anywhere .java文件需要先<h3>编译</h3>----》.class文件 Windows的DOS命令,其实是Windows系统的cmd命令,它是由原来的MS-DOS系统保留下来的。&nb
java Stable悬浮窗 编码字符集 源文件 Windows