一个显示多少时间之前的函数
来源:互联网 发布:网站维护源码 编辑:程序博客网 时间:2024/04/28 17:23
收藏一个老外写的,专门用来显示比如“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;
请注意其中对英文单复数时间的处理:
<?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;
- 一个显示多少时间之前的函数
- PHP中"多少时间之前的"做法
- php把时间戳转换成多少时间之前函数
- 安卓中制造一个显示使用了多少时间的方法
- Python获取多少小时之前的时间并格式化
- js 显示时间戳转化为几分钟之前多久之前方法函数
- 之前一篇文章的功能增加,新增了时间显示
- 一个显示时间的ClockPanel
- 一个简单的时间显示
- 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示“××年还剩××天××时××分××秒”
- 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示"××年还剩××天××时××分××秒"
- android的一个activity的生命周期中有多少函数
- 关于时间的问题,多少多少年前
- 一个显示系统时间的例子
- mysql 显示一个月后的时间
- mysql 显示一个月后的时间
- 一个很好的js时间显示
- 一个下拉显示时间的控件
- Linux slab 分配器剖析
- jquery实现gmail那样的直接上传效果
- (转)addslashes与mysql_real_escape_string的区别
- (转)php脚步的定时任务
- Android framebuffer 截屏原理
- 一个显示多少时间之前的函数
- linux大内存页 HugePages on Linux
- 三种使用php namespace的方法
- 《深入浅出JBPM》这书,也太那个了吧
- TOMCAT 6.0.32,7.08同时发布
- 利用google的短网址服务去实现简化网址
- 将gridview导出到excel,world,pdf的小结
- android中下载文件到sdcard和进度条小结
- Android中自定义对话框小结