php中将时间差转换为字符串提示的实现代码


Posted in PHP onAugust 08, 2011

如微博

这看起来更加人性化,好吧,上代码

<?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 相关文章推荐
php xml文件操作代码(一)
Mar 20 PHP
ajax php 实现写入数据库
Sep 02 PHP
php Smarty date_format [格式化时间日期]
Mar 15 PHP
php编写一个简单的路由类
Apr 13 PHP
PHP中调用ASP.NET的WebService的代码
Apr 22 PHP
PHP备份数据库生成SQL文件并下载的函数代码
Feb 05 PHP
解析PHP中一些可能会被忽略的问题
Jun 21 PHP
ThinkPHP之N方法实例详解
Jun 20 PHP
PHP中的socket_read和socket_recv区别详解
Feb 09 PHP
Discuz!X中SESSION机制实例详解
Sep 23 PHP
深入理解PHP的远程多会话调试
Sep 21 PHP
PHP实现的猴王算法(猴子选大王)示例
Apr 30 PHP
兼容firefox,chrome的网页灰度效果
Aug 08 #PHP
用php解析html的实现代码
Aug 08 #PHP
php中设置多级目录session的问题
Aug 08 #PHP
ThinkPHP 防止表单重复提交的方法
Aug 08 #PHP
ThinkPHP与PHPExcel冲突解决方法
Aug 08 #PHP
让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享
Aug 08 #PHP
php Smarty初体验二 获取配置信息
Aug 08 #PHP
You might like
PHP怎样调用MSSQL的存储过程
2006/10/09 PHP
实现在同一方法中获取当前方法中新赋值的session值解决方法
2014/06/26 PHP
PHP中iconv函数转码时截断字符问题的解决方法
2015/01/21 PHP
ThinkPHP数据操作方法总结
2015/09/28 PHP
PHP制作用户注册系统
2015/10/23 PHP
修改WordPress中文章编辑器的样式的方法详解
2015/12/15 PHP
PHP模板引擎Smarty自定义变量调解器用法
2016/04/11 PHP
JavaScript 学习笔记(四)
2009/12/31 Javascript
qTip2 精致的基于jQuery提示信息插件
2012/02/17 Javascript
jQuery中Ajax的load方法详解
2015/01/14 Javascript
JQuery实现样式设置、追加、移除与切换的方法
2015/06/11 Javascript
AngularJS指令详解及示例代码
2016/08/16 Javascript
详解Vue用自定义指令完成一个下拉菜单(select组件)
2017/10/31 Javascript
Vue.js添加组件操作示例
2018/06/13 Javascript
JavaScript创建对象方法实例小结
2018/09/03 Javascript
小程序实现抽奖动画
2020/04/16 Javascript
mocha的时序规则讲解
2019/02/16 Javascript
Python实现将SQLite中的数据直接输出为CVS的方法示例
2017/07/13 Python
TensorFlow深度学习之卷积神经网络CNN
2018/03/09 Python
200行python代码实现2048游戏
2019/07/17 Python
利用python实现AR教程
2019/11/20 Python
Python 读取xml数据,cv2裁剪图片实例
2020/03/10 Python
django 解决model中类写不到数据库中,数据库无此字段的问题
2020/05/20 Python
详解pandas获取Dataframe元素值的几种方法
2020/06/14 Python
tensorflow 动态获取 BatchSzie 的大小实例
2020/06/30 Python
css3一个简易的 LED 数字时钟实现方法
2020/01/15 HTML / CSS
HTML5之SVG 2D入门10—滤镜的定义及使用
2013/01/30 HTML / CSS
远程Wi-Fi宠物监控相机:Petcube
2017/04/26 全球购物
Myprotein俄罗斯官网:欧洲第一运动营养品牌
2019/05/05 全球购物
经济担保书范文
2014/04/02 职场文书
投资协议书范本
2014/04/21 职场文书
毕业论文评语大全
2014/04/29 职场文书
依法行政工作汇报材料
2014/10/28 职场文书
扬州个园导游词
2015/02/06 职场文书
2016年6月份红领巾广播稿
2015/12/21 职场文书
解决Nginx 配置 proxy_pass 后 返回404问题
2021/03/31 Servers