Input the UNIX timestamp of the article stored in the database to convert it to a prompt such as a few minutes ago, a few hours ago, or a few days ago. For example, Weibo seems more user-friendly. okay, go to the code.
The code is as follows:
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
*/
}
?>