2019-03-15 Pujan Niroula 1 Minute(s) Read

[How to] Create Time Ago in PHP

img-php time ago function thumbnail.jpg

 Hello there,
After a long time I am back with simple and little tricky but widely used code. If you want to create time ago system just like facebook. This is just a simple and tiny function which will convert PHP timestamp.

If you stored time with time() function in then this will be very easy just use the following function. Time() function returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

function get_time($t_time) {
    $pt = time() - $t_time;
    if ($pt>31536000)
        $p = date("Y-M-j",$t_time);
    elseif ($pt>=86400)
        $p = date("M-j",$t_time);
    elseif ($pt>=3600)
        $p = (floor($pt/3600))."h ago";
    elseif ($pt>=60)
        $p = (floor($pt/60))."m ago";
    elseif ($pt<2)
        $p="Just now";
    else
        $p = $pt."s ago";
    return $p;
};

If current time is greater than an year(31536000 seconds) then this will return full date  yy-mm-dd similarly if current time is less than an year  this will return just  mm-dd format furthermore it will return  hour, minute and second ago.

To call the function just type get_time(time()-100);

Thanks for reading :)

Tags:php, how to, time,
Comments (6) Add New Comment
najlepszy sklep2024-03-02 6:28 PM

Somebody necessarily lend a hand to make severely posts I'd state. This is the very first time I frequented your website page and to this point? I surprised with the analysis you made to make this particular post amazing. Excellent process! I saw similar here: sklep internetowy and also here: najlepszy sklep

Reply
najlepszy sklep2024-03-02 6:22 PM

Appreciate the recommendation. Will try it out. I saw similar here: sklep online and also here: najlepszy sklep

Reply
Haley2024-03-02 12:51 PM

For most up-to-date news you have to go to see the web and on the web I found this website as a most excellent web page for most recent updates. I saw similar here: Najlepszy Sklep and also here: sklep internetowy

Reply
ecommerce2024-02-28 9:50 AM

Hi, I do believe this is a great web site. I stumbledupon it ;) I may come back once again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to help others. I saw similar here: %random_link% and also here: %random_link%

Reply
twinning2024-02-25 7:18 AM

I have rea? so many content on the topic of the blogger love?s however this post is actua?ly a ?leasant post, keep it up.

Reply
Yoho Cloud2023-05-09 7:10 AM

It help us alots and thanks for sharing such master piece .

Reply