glassfish Posted December 1, 2014 Share Posted December 1, 2014 Hey, does anybody know a good function, where I can have the trendy "time ago" functionality? A good tutorial would be cool, too. I would be using the datetime function off MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/292833-php-datetime-ago-function/ Share on other sites More sharing options...
Ch0cu3r Posted December 1, 2014 Share Posted December 1, 2014 You would get a better results if you goggled that term Quote Link to comment https://forums.phpfreaks.com/topic/292833-php-datetime-ago-function/#findComment-1498223 Share on other sites More sharing options...
glassfish Posted December 2, 2014 Author Share Posted December 2, 2014 I did google, I found various ones, but not necessarily something I liked too much. Then I found one and I did not know what "granulaty" is. Quote Link to comment https://forums.phpfreaks.com/topic/292833-php-datetime-ago-function/#findComment-1498257 Share on other sites More sharing options...
Barand Posted December 2, 2014 Share Posted December 2, 2014 Granularity determines the smallest interval you are interested in. If an event occurred 3 days 5 hours 12 minutes 25 seconds ago would you want to show that, or would it be 3 days ago 3 days 5 hours ago 3 days 5 hours 12 minutes ago You might just want 3 days, but what if the event was only 5 hours 12 minutes 25 seconds ago, what then? 5 hours ago? today? Have a look at DateTime (particulary DateTime::diff() ) and DateInterval classes. Quote Link to comment https://forums.phpfreaks.com/topic/292833-php-datetime-ago-function/#findComment-1498312 Share on other sites More sharing options...
Monkuar Posted December 3, 2014 Share Posted December 3, 2014 (edited) You can go Barad's route and craft one up, but if you need a good quick one: https://github.com/jimmiw/php-time-ago 0 <-> 29 secs # => less than a minute 30 secs <-> 1 min, 29 secs # => 1 minute 1 min, 30 secs <-> 44 mins, 29 secs # => [2..44] minutes 44 mins, 30 secs <-> 89 mins, 29 secs # => about 1 hour 89 mins, 29 secs <-> 23 hrs, 59 mins, 29 secs # => about [2..24] hours 23 hrs, 59 mins, 29 secs <-> 47 hrs, 59 mins, 29 secs # => 1 day 47 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days 29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months 1 yr <-> 2 yrs minus 1 secs # => about 1 year 2 yrs <-> max time or date # => over [2..X] years Here is a more lightweight and simpler approach: http://codeforgeek.com/2014/10/time-ago-implementation-php/ Edited December 3, 2014 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/292833-php-datetime-ago-function/#findComment-1498340 Share on other sites More sharing options...
glassfish Posted December 6, 2014 Author Share Posted December 6, 2014 Thanks Monkuar, I tried the second link and it worked out great. Quote Link to comment https://forums.phpfreaks.com/topic/292833-php-datetime-ago-function/#findComment-1498766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.