PHP Tutorials Show articles a few minutes ago, a few hours ago, a few days ago released class
When the article was published, the Unix timestamp was converted to a hint such as a few minutes ago, a few hours ago, a few days ago. such as micro-blog
This looks more humane.
1 <?php
2 class Timeago
3 {
4 static $timeagoObject;
5 Private $rustle;
6 private $unit;
7
8 Private Function __construct ()
9 {
10
11}
Private Function __clone () {}
The public static function GetObject ()
14 {
if (! (Self:: $timeagoObject instanceof Self))
Self:: $timeagoObject = new Timeago ();
17
Return self:: $timeagoObject;
19}
Private Function Count_int ($unix _c)//main function
21 {
if (! (Isset ($unix _c) | | | | is_numeric ($unix _c))
' don ' t find parameter ';
24
$d = Time ()-$unix _c; $d-unix Time Difference value
$d _int = (int) floor ($d/60); Minimum unit--minutes UNIX/60
27
$this->unit = 0; Is Minutes,hour or day?
29
if ($d _int <) {//minutes in one hour 3600
$this->rustle = $d _int;
$this->unit = 1;
33}
Or else if ($d _int < 720) {//hour in one day 3600*12
$this->rustle = Floor ($d _int/60);
$this->unit = 2;
37}
/else if ($d _int < 7200) {//day in ten days 3600*12*10
$this->rustle = Floor ($d _int/720);
$this->unit = 3;
41}
else{
$this->rustle = $d;
$this->unit = 4;
45}
46}
The Public Function piece_str ($C)
48 {
$this->count_int ($C);
50
Wuyi $u = ';
Switch ($this->unit)
53 {
Case 1:
$u = ' minute ';
a break;
Case 2:
$u = ' hour ';
The break;
Case 3:
$u = ' Day ';
a break;
Case 4:
$u = ';
The break;
Case 0:
"Return" Sorry, get time is fail ';
68}
if ($this->unit < 4)
70 {
if ($this->rustle > 1)
A return (string) $this->rustle. $u. ' s ago ';
Or else if ($this->rustle = 1)
The return (string) $this->rustle. $u. ' Ago ';
Or else
"Just Now";
77}
78}
* Example: $ago = Timeago::getobject ();
* Echo $ago->piece_str ($unix);
Bayi *//2 days ago
82 */
83}
?>