relative dates? |
[eluser]jvittetoe[/eluser]
I'm trying to achieve something like so, "Posted 35 minutes ago". Has anybody created a function to output something like this.
[eluser]Michael Wales[/eluser]
Date Helper: timespan() It's not the most beautiful solution (the returned text can get into way more detail than anyone would really care about), but it's your fastest solution. You could also rip the code out and use it to create your own helper, more personalized to your needs. I do this quite often with this particular helper.
[eluser]tonanbarbarian[/eluser]
http://userguide.codeigniter.local/helpe...elper.html timespan() Formats a unix timestamp so that is appears similar to this: 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes Code Igniter is way ahead of you
[eluser]zdknudsen[/eluser]
You'd most likely want to do a check first (e.g. if it was posted more than a week ago) and then either use the timespan or an exact date based on that.
[eluser]Derek Allard[/eluser]
I almost always make a copy of the date helper in application/helpers so that I can whittle away the seconds and such from timespan. For example, in Bamboo I only care about how many years, months and days overdue something is, so I get rid of hours, minutes and seconds in the local date helper. That is probably something that would be nice if it was configurable... like extra arguments passed into the helper function. Hm....
[eluser]Michael Wales[/eluser]
I don't think it need to be configurable... just common "sensical" For the typical uses of this function: 1. Anything less than 5 minutes doesn't matter - just now will suffice. 2. Hours/Minutes don't matter, once you hit days. 3. Days don't matter once you hit months 4. Months don't matter once you hit years. Don't get me wrong - I've generalized the function quite a bit. But really, this is almost always used for "posted ago" type situations, as a quick reference. We'll take BambooInvoice for example (although I have limited experience with it). Let's say we have a page with overdue invoices... Quote:000 Company Name More than 2 years overdue Compared to (if we didn't generalize so much and just used years, months, days (which is still better than how timespan is now): Quote:000 Company Name 2 years, 4 months, 10 days overdue Another example, a forum/blog: Quote:Posted more than 1 year ago I think what it really comes down to - this is used for quick perception of a timeframe. Nothing legitimate, nothing you would base math off of - just a mental reference. As time goes on, the amount of detail in which you care about decreases. If I'm invoicing someone do I really care they are 1 year, 6 months late (as opposed to more than 1 year)? Probably not - they are hella-late and I am taking them to small claims court regardless. I'm not saying this is the end-all solution, but I believe it's a definite improvement. Some rounding could be involved as well: 1 year, 8 months = almost 2 years 3 months, 27 days = almost 4 months etc.
[eluser]Derek Allard[/eluser]
Intelligent defaults are important, but without configuration, your "sensical" settings are just as useless to me as the original assumptions. For example, I personally want to see "3 weeks 2 days" instead of "23 days". I can see that not everyone would, so probably a good case of configuration. Same thing with hours and minutes (and seconds). I don't care about that, but I could imagine situations where a developer would want it. That said though, your central point of more intelligent defaults is well taken
[eluser]jvittetoe[/eluser]
one more thing, how can i convert a mysql timestamp into a unix timestamp.
[eluser]Derek Allard[/eluser]
Clemens wrote this, I can't take credit. Add it to your date helper and save it in application/helpers Code: // ------------------------------------------------------------------------
[eluser]Mark van der Walle[/eluser]
Whats wrong with the good old strtotime function PHP has? |
Welcome Guest, Not a member yet? Register Sign In |