0

Looking for a lightweight function that would convert this date as it is displayed "Thu Sep 19 08:43:29 +0000 2013"

Any ideas?

Glavić
  • 42,781
  • 13
  • 77
  • 107
ghghjk
  • 223
  • 4
  • 11
  • 2
    You need to provide more information than this. For example: What format do you want the "time ago" in? What have you attempted so far to achieve this? (StackOverflow isn't here to do you work for you.) – John Parker Sep 19 '13 at 09:50

3 Answers3

7

Time ago function

function time_ago($date) {
    if (empty($date)) {
        return "No date provided";
    }
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60", "60", "24", "7", "4.35", "12", "10");
    $now = time();
    $unix_date = strtotime($date);
// check validity of date
    if (empty($unix_date)) {
        return "Bad date";
    }
// is it future date or past date
    if ($now > $unix_date) {
        $difference = $now - $unix_date;
        $tense = "ago";
    } else {
        $difference = $unix_date - $now;
        $tense = "from now";
    }
    for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if ($difference != 1) {
        $periods[$j].= "s";
    }
    return "$difference $periods[$j] {$tense}";
}

To use this function, simply call:

<?php echo time_ago($mydate); ?>

source

Gogol
  • 3,033
  • 4
  • 28
  • 57
Kosh
  • 6,140
  • 3
  • 36
  • 67
1

You can use the handy DateTime class :

$oDate = new DateTime('Thu Sep 19 08:43:29 +0000 2013');
$oNow = new DateTime();
$oInterval = $oDate->diff($oNow);
echo $oInterval->format('%R%a days');

This will display the difference between now and the date, in days :

+0 days

Nassim
  • 240
  • 1
  • 4
0

Use example :

echo time_elapsed_string('Thu Sep 19 08:43:29 +0000 2013');
echo time_elapsed_string('Thu Sep 19 08:43:29 +0000 2013', true);

Output :

1 hour ago
1 hour, 35 minutes, 7 seconds ago

Link to the function.

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107