PHP Display article a few minutes ago, a few hours ago, a few days ago released class

Source: Internet
Author: User

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}
?>

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.