php中将时间差转换为字符串提示的实现代码
2015-01-24信息快讯网
通过传入数据库中存储的文章发表时的UNIX时间戳,来转化为例如 几分钟前,几小时前,几天前 这样的提示。
如微博这看起来更加人性化,好吧,上代码
<?php
class timeAgo
{
static $timeagoObject;
private $rustle;
private $unit;
private function __construct()
{
}
private function __clone(){ }
public static function getObject()
{
if(! (self::$timeagoObject instanceof self) )
self::$timeagoObject = new timeAgo();
return self::$timeagoObject;
}
private function count_int($unix_C) // main function
{
if(! (isset($unix_C) || is_numeric($unix_C)) )
return 'don\'t find parameter';
$d = time()-$unix_C ; // $d - unix time difference value
$d_int =(int)floor($d/60) ; // minimum unit -- minutes unix/60
$this->unit = 0 ; // is minutes,hour or day?
if($d_int < 60){ // minutes in one hour 3600
$this->rustle = $d_int;
$this->unit = 1;
}
else if($d_int < 720){ //hour in one day 3600*12
$this->rustle = floor($d_int/60);
$this->unit = 2 ;
}
else if($d_int < 7200){ //day in ten days 3600*12*10
$this->rustle = floor($d_int/720);
$this->unit = 3 ;
}
else{
$this->rustle = $d ;
$this->unit = 4 ;
}
}
public function piece_str($C)
{
$this->count_int($C);
$u = '';
switch( $this->unit )
{
case 1:
$u = 'minute';
break;
case 2:
$u = 'hour';
break;
case 3:
$u = 'day';
break;
case 4:
$u = '';
break;
case 0:
return 'sorry , get time is fail';
}
if($this->unit < 4)
{
if($this->rustle > 1)
return (string)$this->rustle.$u.'s ago';
else if($this->rustle == 1)
return (string)$this->rustle.$u.'ago';
else
return 'Just now';
}
}
/* example: $ago = timeAgo::getObject();
* echo $ago->piece_str($unix);
* // 2 days ago
*/
}
?>
关于PHP的相似度计算函数:levenshtein的使用介绍
phpmyadmin显示utf8_general_ci中文乱码的问题终级篇
简单实现限定phpmyadmin访问ip的方法
Apache中php.ini的设置方法
浅谈apache和nginx的rewrite的区别
PHP-redis中文文档介绍
php中使用redis队列操作实例代码
PDO版本问题 Invalid parameter number: no parameters were bound
php中unlink()、mkdir()、rmdir()等方法的使用介绍
PHP提示Notice: Undefined variable的解决办法
PHP学习笔记 IIS7下安装配置php环境
php模拟js函数unescape的函数代码
记录PHP错误日志 display_errors与log_errors的区别
PHP中数字检测is_numeric与ctype_digit的区别介绍
PHP 中关于ord($str)>0x80的详细说明
PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)
使用PHP实现密保卡功能实现代码<打包下载直接运行>
php简单的会话类代码
兼容firefox,chrome的网页灰度效果
ThinkPHP 防止表单重复提交的方法
ThinkPHP与PHPExcel冲突解决方法
让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享
discuz程序的PHP加密函数原理分析
使用ThinkPHP自带的Http类下载远程图片到本地的实现代码
linux下使用ThinkPHP需要注意大小写导致的问题
PHP源代码数组统计count分析
PHP 删除文件与文件夹操作 unlink()与rmdir()这两个函数的使用
PHP Undefined index报错的修复方法
php自定义函数call_user_func和call_user_func_array详解
PHP array_multisort()函数的使用札记
php设计模式 Visitor 访问者模式
从康盛产品(discuz)提取出来的模板类
php whois查询API制作方法
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
php中$this->含义分析
php self,$this,const,static,->的使用
PHP nl2br函数 将换行字符转成 <br>