-3

Possible Duplicate:
PHP: producing relative date/time from timestamps

Given a specific DateTime value, how do I display relative time, like

  • 35 minutes ago
  • 2 hours ago
  • 3 days ago
  • 1 month ago

etc, etc...?

mysql_query("UPDATE users SET lastlogin = ".time()." WHERE id = ".$userID); 
Community
  • 1
  • 1
SDZ
  • 726
  • 2
  • 8
  • 21
  • 1
    Its just an example. Thanks tho. – SDZ Mar 08 '12 at 15:10
  • 3
    @JackManey I don't think you can tell someone that their code is vulnerable to SQL Injection when you have no clue how they are getting the variable. – jprofitt Mar 08 '12 at 15:12
  • 1
    Thanks for the concern but its just an example of how I will add it to the DB – SDZ Mar 08 '12 at 15:13
  • 4
    @JackManey And what if in the code, `$userId` is hardcoded as a number? You're *speculating* that his code is vulnerable without seeing if it **actually** is. – jprofitt Mar 08 '12 at 15:13
  • 1
    Everyone is so negative this mornign. Good bye stack overflow. See you when you've calmed down a bit. – Milo LaMar Mar 08 '12 at 15:21
  • 1
    I agree. I have 2 down votes for no reason. – SDZ Mar 08 '12 at 15:26

3 Answers3

3

this may help you

function time_ago_en($time)
{
    if(!is_numeric($time))
        $time = strtotime($time);

    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "age");
    $lengths = array("60","60","24","7","4.35","12","100");

    $now = time();

    $difference = $now - $time;
    if ($difference <= 10 && $difference >= 0)
        return $tense = 'just now';
    elseif($difference > 0)
        $tense = 'ago';
    elseif($difference < 0)
        $tense = 'later';

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    $period =  $periods[$j] . ($difference >1 ? 's' :'');
    return "{$difference} {$period} {$tense} ";
}

Usage:

<?php
echo time_ago_en(time() - 300); // 5 minutes ago
?>
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
0

strtotime is a nice function

let me be more specific

strtotime(-5 days);

or

strtotime(-1 month);

There is an optional 2nd argument which is another time stamp of the time you want to start at, giving your relative time. Why the downvote?

Milo LaMar
  • 2,146
  • 2
  • 14
  • 25
0
<?php
function relativeTime($time = false, $limit = 86400, $format = 'g:i A M jS') {
    if (empty($time) || (!is_string($time) &amp;&amp; !is_numeric($time))) $time = time();
    elseif (is_string($time)) $time = strtotime($time);

    $now = time();
    $relative = '';

    if ($time === $now) $relative = 'now';
    elseif ($time > $now) $relative = 'in the future';
    else {
        $diff = $now - $time;

        if ($diff >= $limit) $relative = date($format, $time);
        elseif ($diff < 60) {
            $relative = 'less than one minute ago';
        } elseif (($minutes = ceil($diff/60)) < 60) {
            $relative = $minutes.' minute'.(((int)$minutes === 1) ? '' : 's').' ago';
        } else {
            $hours = ceil($diff/3600);
            $relative = 'about '.$hours.' hour'.(((int)$hours === 1) ? '' : 's').' ago';
        }
    }

    return $relative;
}
?>

Source

F.P
  • 17,421
  • 34
  • 123
  • 189