-1

I have stored a date in database as "15-10-2012",while showing in the front end when the date is not older than one day.I need to show 2 min age , 5 hours ago etc.

how can i achieve this? thanks in advance.

Input

15-10-2012

Output

"2 days ago"  
Manoj
  • 373
  • 3
  • 10
  • [calculating and showing a date as 'secs ago', 'mins ago', 'hours ago' etc](http://stackoverflow.com/q/2452010) – Pekka Oct 16 '12 at 10:12
  • [Calculate time difference between two dates, and present the answer like "2 days 3 hours ago"](http://stackoverflow.com/q/432398) – Pekka Oct 16 '12 at 10:12
  • possible duplicate of [Calculate time difference between two dates, and present the answer like "2 days 3 hours ago"](http://stackoverflow.com/questions/432398/calculate-time-difference-between-two-dates-and-present-the-answer-like-2-days) – MrCode Oct 16 '12 at 10:15

3 Answers3

1

See this article http://www.ozzu.com/programming-forum/mysql-selecting-timestamp-from-database-now-minute-t82112.html

And take a look at MYSQL's INTERVAL http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

I think this will help you: WHERE UNIX_TIMESTAMP(your_date) > (NOW() - INTERVAL 5 MINUTE)

Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
0

First calculate a unix timestamp from the date, then substract from current time and turn the difference in minutes/hours/days.

list($day,$month,$year) = explode('-', $date);
$time = mktime(0,0,0,$month,$day,$year);
$now = time();
$difference = $now - $time;
$minutes = intval($difference / 60);

$hours = intval($minutes / 60 ); 

if ($hours > 24) {
   $days = intval($hours / 24);
}
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
0

(assuming day-month-year)

2 minutes ago:

echo date('j-n-Y', strtotime('15-10-2012') - 120);

Or:

echo date('j-n-Y', strtotime('15-10-2012 -2 minutes'));

Really your starting date should be a timestamp down to the second. If storing just the day/month/year then you'd need to assume each date starts at 00:01

Stevie
  • 245
  • 4
  • 9