Skip to content
This repository

Nicer fuzzy time than timespan() #309

Closed
philsturgeon opened this Issue · 23 comments
Phil Sturgeon

Timespan is really handy but often too detailed. In several projects I find myself writing stuff like this:

$tweet->timespan = strtolower(current(explode(',', timespan(strtotime($tweet->created_at))))).' ago';

This produces "4 hours ago" or "1 week ago" which is a bit more in line with how Twitter does it.

Obviously I could write a new function, but I am a bit lazy with things like this. I'm also not sure if it could be added into the existing function as a 3rd param, or what the name of a new function should be. Whatdyathink?

Joël Cox

This is a function I keep adding to my projects, too. I always use time_ago() as function name.

Phil Sturgeon

Could you post it up here?

Eric Barnes
Collaborator

Here is one I used in the past - https://gist.github.com/1173330

Joël Cox

It's not as nice as yours and involves as bunch of switch statements. I round all quantities lower than 2 to 1 (so 119 seconds would become 'a minute ago').

See gist

Eric Barnes
Collaborator

If I am not mistaken mine came from zack @ forrest. Not sure where he found it.

Eric Barnes
Collaborator

I actually forgot about timespan. :) I would also like to see the ability to do this with future dates.

That could be beneficial for scheduling items such as blog posts in a cms. "Publish in 1 day 5 mins".

Joël Cox
Marco Monteiro

Yeah, using that in the Future would be awesome... :)

Timothy Seebus

This is a function i wrote in 2008 and does the calculations for dates in the past and future (it needs to be cleaned up a bit ) thought it might give people a idea - https://gist.github.com/1173418

Kyle Farris

I have a Date library that I've been using for a while now that has lots of useful methods in it. It would need some serious code review but it works for me really well (some method names would need to be changed and maybe a few removed since there are similar one already in the Date helper). What are you guys' feelings on adding a new Date library to the core (not that I'm suggest the file I'm linking to by any means...)?

https://gist.github.com/1173470

Dennis Rasmussen

My input: https://gist.github.com/1173514

Edit: Another file below the original which lists the plural words in an array instead for easier localization.

Dennis Rasmussen

@marcoscoelho your point with that link is?

Kyle Farris

@marcoscoelho, that feature is only available in PHP >= 5.3 which would alienate a lot of people. It could be incorporated but would have to have checks in place to make sure it was there first.

Marcos Coelho

@DennisRas
Sorry, not sure if I'm aware of this talk - maybe due to my poor interpretation, but the function of php does it. But really @kylefarris as you said, is incompatible with CI that still does not encourage the php upgrade.. i'm thinking as php, that said in their latest release that the ideal would be to think now >= 5.3.

Phil Sturgeon

We're not upgrading the requirements to PHP 5.3 any time soon, especially not for such a trivial issue. I just want a little bit of fuzzy time. Let's not go mad! :)

Dennis Rasmussen

@marcoscoelho, my bad actually. I thought it was the link to the date function :) Shoot me!
Anyway my suggestion is still https://gist.github.com/1173514

Kyle Farris

@DennisRas, haha, when I read the first thing you said to marco, I thought to myself... @DennisRas is kind of a jerk. Honest mistake, though... I forgive you.

UPDATE: By the way, I think your function is perfect.

Marcos Coelho

@DennisRas your suggestion looks more sexy :+1: ... sorry all my equivocal. ;p

Prashant Sugand

I've also written one a while ago, this is what I use. I've saved it as a helper.

https://gist.github.com/1185750

Andrey Andreev
Collaborator

How about this one: #1165

David Parkinson

Which one does everyone suggest using?

Cory Kennedy-Darby

@philsturgeon Pull request was accepted for #1165 this has been resolved; Close :)?

Andrey Andreev narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.