Prompt for php to convert time difference to string

Source: Internet
Author: User

This looks more user-friendly. Okay, go to the code. Copy codeThe Code is as follows: <? 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
*/
}
?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.