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

相关文章

PHP实用小技巧之调用录像的方法

主要功能 把你实际的调用操作录下来,然后在你想要的地方重新调用 和匿名函数的作用基本一样,暂存你的调用操作 一般用于链式调用, 然后实际作用于你想要操作的对象上面 好像和没说一样...

详解WordPress中调用评论模板和循环输出评论的PHP函数

comments_template comments_template 函数是一个调用评论模板的函数,使用起来很简单,与get_header()等函数一样,是一个include文件类函数...

PHP中有关长整数的一些操作教程

PHP中有关长整数的一些操作教程

前言 在PHP,数字类型只有int和float两种,它们的位数取决于系统,而且没有uint,所以跟其它系统通信的时候就有诸多不便。如果int溢出,则自动转换为float,用科学计数法来表...

PHP中对缓冲区的控制实现代码

大家在使用PHP的过程中不免要使用到header和setcookie两个函数,这两个函数会发送一段文件头信息给浏览器,但是如果在使用这两个函数之前已经有了任何输出(包括空输出,比如空格,...

php简单实现文件或图片强制下载的方法

本文实例讲述了php简单实现文件或图片强制下载的方法。分享给大家供大家参考,具体如下: //下载 function downregcaseAction() { $file="up...