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

yipeiwu_com5年前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字符串中插入子字符串方法总结 原创

本文实例讲述了PHP字符串中插入子字符串方法。分享给大家供大家参考,具体如下: 首先来看看一个网上常见的方法: 方法一:字符串遍历 function str_insert($str,...

PHP中STDCLASS用法实例分析

本文实例讲述了PHP中STDCLASS用法。分享给大家供大家参考,具体如下: PHP中STDCLASS在我们开发应用中使用到的不多,但是PHP中STDCLASS作用是非常的大的,下面我们...

根据key删除数组中指定的元素实现方法

php数组中元素的存在方式是以键值对的方式('key'=>'value'),有时候我们需要根据键删除数组中指定的某个元素。 function bykey_reitem($a...

PHP中数组的三种排序方法分享

一、冒泡排序法 说明:找到最大的数,排列到最后面,然后继续找 例: 复制代码 代码如下: $arr = array(3,5,-1,0,2); for($i=0;$i<count($...

PHP 应用容器化以及部署方法

PHP 应用容器化以及部署方法

PHP 是世界上最好的语言。 经典的 LNMP(linux + nginx + php + mysql)环境有很多现成的部署脚本,但是在 Docker 盛行的今天,依然有很多同学在如何...