PHP converts the time difference to a string hint _php tutorial

Source: Internet
Author: User
This looks more humane, okay, on the code
Copy CodeThe 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 <) {//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
*/
}
?>

http://www.bkjia.com/PHPjc/324289.html www.bkjia.com true http://www.bkjia.com/PHPjc/324289.html techarticle this looks more humane, OK, the code copy code is as follows: PHP class Timeago {static $timeagoObject; private $rustle; private $unit; Private functi On __construct ( ...

  • 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.