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 (9) Add New Comment
yupoo2024-04-27 12:58 AM

Hello to every one, because I am in fact eager of reading this blog's post to be updated daily. It contains fastidious information.

Reply
yupoo2024-04-27 12:57 AM

Hello to every one, because I am in fact eager of reading this blog's post to be updated daily. It contains fastidious information.

Reply
Gsa ser link list2024-04-21 2:52 PM

Attempt to room it out so your links don't obtain deleted or anything like that.

Reply
Gsa ser link list2024-04-21 2:11 PM

You can avoid building websites if the public relations is under a specific number or not.

Reply
Gsa ser link list2024-04-21 5:55 AM

Discover the vital differences to see why it's the top selection for organizations and people.

Reply
website2024-04-20 9:50 PM

Hi! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup. Do you have any solutions to prevent hackers?

Reply

Hello, put your money into my hand-drawn NFTs. They are unique and original. Invest safely in an art medium, excellent in the long run. Don't hesitate to contact me: michel710@tutanota.com Thank you

Reply
Aayush Shrestha2024-03-07 7:23 AM

Will obviously use this the next time ! Thanks for the information brother

Reply
Yoho Cloud2023-05-09 7:10 AM

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

Reply