php将时间差转换为字符串提示


Posted in PHP onSeptember 07, 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 相关文章推荐
一个简单的MySQL数据浏览器
Oct 09 PHP
mysql下创建字段并设置主键的php代码
May 16 PHP
PHP缓存技术的多种方法小结
Aug 14 PHP
php魔术方法与魔术变量、内置方法与内置变量的深入分析
Jun 03 PHP
PHP计算指定日期所在周的开始和结束日期的方法
Mar 24 PHP
PHP使用file_get_content设置头信息的方法
Feb 14 PHP
PHP CURL post数据报错 failed creating formpost data
Oct 16 PHP
PHP编程中的Session阻塞问题与解决方法分析
Aug 07 PHP
PHP实现基于状态的责任链审批模式详解
May 31 PHP
Yii框架日志操作图文与实例详解
Sep 09 PHP
laravel接管Dingo-api和默认的错误处理方式
Oct 25 PHP
php设计模式之观察者模式实例详解【星际争霸游戏案例】
Mar 30 PHP
php 中英文语言转换类
Sep 07 #PHP
php继承的一个应用
Sep 06 #PHP
php 抽象类的简单应用
Sep 06 #PHP
PHP中PDO基础教程 入门级
Sep 04 #PHP
PHP中PDO的错误处理
Sep 04 #PHP
php中将网址转换为超链接的函数
Sep 02 #PHP
php ajax 静态分页过程形式
Sep 02 #PHP
You might like
德生S2000南麂列岛台湾FM收听记录
2021/03/02 无线电
PHP使用DES进行加密与解密的方法详解
2013/06/06 PHP
PHP读取大文件的多种方法介绍
2016/04/04 PHP
php 生成签名及验证签名详解
2016/10/26 PHP
Prototype使用指南之ajax
2007/01/10 Javascript
jquery蒙版控件实现代码
2010/12/08 Javascript
使用jquery实现IE下按backspace相当于返回操作
2014/03/18 Javascript
浅谈javascript回调函数
2014/12/07 Javascript
了不起的node.js读书笔记之例程分析
2014/12/22 Javascript
BootStrap的alert提示框的关闭后再显示怎么解决
2016/05/17 Javascript
js判断复选框是否选中及选中个数的实现代码
2016/05/30 Javascript
通过修改360抢票的刷新频率和突破8车次限制实现方法
2017/01/04 Javascript
JavaScript多种页面刷新方法小结
2019/04/04 Javascript
多个vue子路由文件自动化合并的方法
2019/09/03 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
2019/09/18 Javascript
原生JS实现无缝轮播图片
2020/06/24 Javascript
如何构建一个Vue插件并生成npm包
2020/10/26 Javascript
[05:09]2016国际邀请赛中国区预选赛淘汰赛首日精彩回顾
2016/06/29 DOTA
[01:05:56]2018DOTA2亚洲邀请赛3月29日 小组赛A组 Newbee VS VG
2018/03/30 DOTA
[01:00:53]OG vs IG 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
详解Python网络爬虫功能的基本写法
2016/01/28 Python
numpy实现合并多维矩阵、list的扩展方法
2018/05/08 Python
Python代码使用 Pyftpdlib实现FTP服务器功能
2019/07/22 Python
Python内置类型性能分析过程实例
2020/01/29 Python
Python获取指定网段正在使用的IP
2020/12/14 Python
俄罗斯最大的香水和化妆品网上商店:Randewoo
2020/11/05 全球购物
澳大利亚电商Catch新西兰站:Catch.co.nz
2020/05/30 全球购物
自荐信格式范文
2013/10/07 职场文书
善意的谎言事例
2014/02/15 职场文书
公司员工活动策划方案
2014/08/20 职场文书
团代会开幕词
2015/01/28 职场文书
第二次离婚起诉书
2015/05/18 职场文书
学习新党章心得体会2016
2016/01/15 职场文书
Windows下使用Nginx+Tomcat做负载均衡的完整步骤
2021/03/31 Servers
Vue和Flask通信的实现
2021/05/19 Vue.js
用JS写一个发布订阅模式
2021/11/07 Javascript