views:

50

answers:

2

I have a table with a datetime field, and I want to pull that time stamp and use PHP to convert it to the nearest largest unit of time. For example, if the entry was made 2 minutes and 36 seconds ago, I want to echo 2 minutes in PHP. If it was 3 hours and 5 minutes ago, I want it to say 3 hours. If it was 6 days and 4 hours and 40 minutes ago, it should say 6 days. You get my drift. And if it's under a minute, just the number of seconds.

I am not familiar at all about any of PHP's date or time functions, so please don't assume I already know anything. Thanks.

+1  A: 

If your field is date or datetime use

  SELECT UNIX_TIMESTAMP(field) FROM...

if your field is stored as an int (seconds since 1970) just use

  SELECT field FROM...

Get that integer value from the database into a PHP variable $secs, this is the number of seconds since 1970. We assume the date is in the past, then

  $diff = time() - $secs;
  if ($diff < 60) echo $diff . ' seconds';
  else {
     $diff = intval($diff / 60);
     if ($diff < 60) echo $diff . ' minutes';
     else {
        $diff = intval($diff / 60);
        if ($diff < 24) echo $diff . ' hours';
        else {
          $diff = intval($diff / 24);
          echo $diff . ' days';
        }
     }
  }
ring0
Great! Thanks for that info, @ring0 it's very clear and it solves my problem :D
RobHardgood
+1  A: 

Here's another function for you:

function relativeTime($timestamp, $format = "Y-m-d")
{
    $difference = time() - $timestamp;
    if ($difference >= 604800) { // 7 days
        return date($format, $timestamp);
    }

    $periods = array("Second", "Minute", "Hour", "Day");
    $lengths = array("60","60","24","7","4.35","12","10");
    $ending = "Ago";
    for($j = 0; $difference >= $lengths[$j]; $j++)
        $difference /= $lengths[$j];
    $difference = round($difference);
    if($difference != 1) $periods[$j].= "s";
    $text = "$difference $periods[$j] $ending";
    return $text;
}

It returns values like "5 Minutes Ago", "3 Days Ago", and "34 Seconds Ago". However if the date is over 7 days old, it just returns the full date, i.e. "2010-04-19".

mellowsoon
Wow, this is another great response! I was just working on the previous response and trying to figure out how I could make it return "day" instead of "days" for 1 day, etc... but this function looks even more comprehensive, and does just what I was trying to figure out. And it adds the word "ago" which I was going to use too. I like the > 7 days part too, that will make everything look a lot cleaner. Thanks a lot!!
RobHardgood
I have one more question @mellowsoon if you're still there. Will I need to change the Y-m-d format for the MySQL datetime which includes minutes hours and seconds?
RobHardgood
Take a look here for a description of how to change $format to suit your needs -> http://us2.php.net/manual/en/function.date.php
mellowsoon
Grr.. I keep forgetting that hitting enter here submits the comment haha.. But change $format to 'Y-m-d H:i:s' for '2010-04-19 12:33:12' format.
mellowsoon
Okay, that's what I thought but I wasn't sure where to look it up. Thanks again! This will save me a ton of stress
RobHardgood
@mellowsoon Uh, for some reason this is only returning "1969-12-31 18:33:30" for some reason... for every entry. I'm trying to figure out why, but if you can tell let me know... edit: it seems to be catching on the 7 days part... trying to figure out why it thinks everything is dated 1969
RobHardgood
Ah, it's because it isn't being converted into unix time.
RobHardgood
Alright, I found another function do that. Sorry for the comments
RobHardgood