2

Possible Duplicate:
PHP: Is there a function that converts a date string in a human readable format?

I have a mysql table with a structure as follows:

table: persons column1: name varchar(60) column2: score int(11) column3: time datetime(60)

I have a form which inserts data into this table: name, score and the current timestamp(NOW()), using php. All the data is displayed with respect to the 'time' column in descending order.

My dilemma is how do I compare the time in the 'time column' with current time and display in human readable format like "5 minutes ago" or "2 hours ago" using php. Is there any function to find time difference in php directly? or should i be using javascript?

Community
  • 1
  • 1
Jenny Dcosta
  • 135
  • 1
  • 4
  • 11
  • http://stackoverflow.com/questions/1763197/php-is-there-a-function-that-converts-a-date-string-in-a-human-readable-format/1763651#1763651 – Lightness Races in Orbit Nov 09 '11 at 18:12
  • @Jenny please lift the green tick from TCS's answer so that he may delete it. It is more of a comment than an answer. He is better than his posted answer. – mickmackusa Jan 16 '18 at 05:07

2 Answers2

0

I suggest you take a look at Symfony DateHelper functions. You should be able to use those functions outside of the Symfony framework and they are quite good at displaying date range and differences.

Source code:

http://trac.symfony-project.org/browser/branches/1.4/lib/helper/DateHelper.php

Hubert Perron
  • 3,022
  • 5
  • 34
  • 28
0

You could use the MySQL TIMEDIFF function to get the difference and then write a simple PHP function to parse this into the relevant human readable output.

Or else load the two datetime values into a PHP DateTime class and then use the diff() method to return a DateInterval class.

liquorvicar
  • 6,081
  • 1
  • 16
  • 21