0

I have a code

echo date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td></tr>';

The result:

23-10-2013 16:28

Is there a way to change into like:

Posted 12 minutes ago

or / &

Just Posted

Whats the easiest way of doing this?

  • How about subtracting current time with the "post date"? Try ! – Raptor Oct 24 '13 at 03:18
  • I'm a php noob... Please help. I can subtract numbers but when it comes to time.... i won't get it –  Oct 24 '13 at 03:19
  • 3
    http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago/18602474#18602474 – Dipesh Parmar Oct 24 '13 at 03:27

2 Answers2

0

To fetch the difference of seconds between current time and posted time:

$number_of_seconds = strtotime("now") - strtotime($posts_row['post_date']);

Then, you can apply your logic to display how many seconds ago, just now, etc.

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

strtotime will help you to convert the date and make operations over it.

function elapsedTimeAgo ($newTime) {

$timeCalc = time() – strtotime($newTime);
$elapsedTimeText = "";

if ($timeCalc > (60*60*24)) {
    $elapsedTimeText = round($timeCalc/60/60/24) . "days ago";
} else if ($timeCalc > (60*60)) {
    $elapsedTimeText = round($timeCalc/60/60) . "hours ago";
} else if ($timeCalc > 60) {
    $elapsedTimeText = round($timeCalc/60) .  "minutes ago";
} else if ($timeCalc > 0) {
    $elapsedTimeText .=  "seconds ago";
} else {
    $elapsedTimeText .=  "Just Posted";
} 
return $elapsedTimeText;
}

echo elapsedTimeAgo($posts_row['post_date']);

N20084753
  • 2,130
  • 2
  • 17
  • 12