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

yipeiwu_com6年前PHP代码库
这看起来更加人性化,好吧,上代码
复制代码 代码如下:

<?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
*/
}
?>

相关文章

jquery+php实现导出datatables插件数据到excel的方法

本文实例讲述了jquery+php实现导出datatables插件数据到excel的方法。分享给大家供大家参考。具体如下: DataTables是一个jQuery的表格插件。这是一个高度...

PHP合并数组函数array_merge用法分析

本文实例讲述了PHP合并数组函数array_merge用法。分享给大家供大家参考,具体如下: 合并数组是把一个数组追加到另一个数组中,主要应用array_merge()函数实现 语法如下...

PHP连接SQLServer2005的实现方法(附ntwdblib.dll下载)

PHP连接SQLServer2005的实现方法(附ntwdblib.dll下载)

php连接sql2005的问题,现在整合,同时把FAQ整合上. 我前面写的教程: 连接前配置系统: 1.检查文件 php5.2.5/ntwdblib.dll 默认下面有一个,不能连接再替...

thinkphp autoload 命名空间自定义 namespace

thinkphp autoload 命名空间自定义 namespace

使用thinkPHP过程中,一些自定义的类库和第三方类库需要找一个合适的位置放置,放到系统默认的org文件夹感觉不太好,破坏了thinkPHP的原生目录。 就看了一下官方手册,可以在模块...

php下实现折线图效果的代码

<?php   Class ImageReport{  var $X;//图片大小X轴  var $Y;//图...