Welcome Guest, Not a member yet? Register   Sign In
relative dates?
#1

[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.
#2

[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.
#3

[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 Smile
#4

[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.
#5

[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....
#6

[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
001 Company Name More than 1 year overdue
002 Company Name 6 months overdue
003 Company Name 3 days 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
001 Company Name 1 year, 1 month, 26 days overdue
002 Company Name 6 months, 4 days overdue
003 Company Name 3 days overdue


Another example, a forum/blog:

Quote:Posted more than 1 year ago
Posted 3 months ago
Posted 8 days ago
Posted just now

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.
#7

[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 Wink
#8

[eluser]jvittetoe[/eluser]
one more thing, how can i convert a mysql timestamp into a unix timestamp.
#9

[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:
// ------------------------------------------------------------------------
/**
* Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to timestamp
*
* Returns the timestamp equivalent of a given DATE/DATETIME
*
* @todo add regex to validate given datetime
* @author Clemens Kofler <[email protected]>
* @access    public
* @return    integer
*/
function mysqldatetime_to_timestamp($datetime = "")
{
  // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
  $l = strlen($datetime);
    if(!($l == 10 || $l == 19))
    {
      return false;
     }

    //
    $date = $datetime;
    $hours = 0;
    $minutes = 0;
    $seconds = 0;

    // DATETIME only
    if($l == 19)
    {
      list($date, $time) = explode(" ", $datetime);
      list($hours, $minutes, $seconds) = explode(":", $time);
    }

    list($year, $month, $day) = explode("-", $date);

    return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
#10

[eluser]Mark van der Walle[/eluser]
Whats wrong with the good old strtotime function PHP has?




Theme © iAndrew 2016 - Forum software by © MyBB