0

I want my clients to be able to ask a question on my website, and after 4 days, for that question to be deleted.

Everytime that question is visited, for the time elapsed since the question was asked, to be displayed.

I assume the easiest way would be to extract the number of seconds since the question was asked, and to display it either in seconds, minutes, hours, or days depending on how long it has been at the time of viewing the question.

I'm using PHP and MySQL. Please help me!

  • Show some of the code you are using right now, so people can base their suggestions on the date library you are using – Pekka Apr 21 '11 at 14:23
  • You can use the date function of PHP: http://php.net/manual/en/function.date.php – tim_a Apr 21 '11 at 14:28
  • Note that time() returns an int which is `the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).`, so simple subtraction works. Also mysql `select datetimefield+0` will return an integer – horatio Apr 21 '11 at 17:19

2 Answers2

1

See my answer here: Calculate number of hours between 2 dates in PHP

Date1 would be the date, when the question was posted and Date2 would be the current date. This version of comparing respects leap-seconds and leap-years and timezones, so it's more detailed than just comparing two unix-timestamps.

Community
  • 1
  • 1
Fidi
  • 5,754
  • 1
  • 18
  • 25
0

If you store the time the question was asked as unix timestamp you can even do it simply by using SQL.

   SELECT subject, body, ...,  SEC_TO_TIME(UNIX_TIMESTAMP() - question_asked_tstamp ) as time_ago FROM `questions`;

If you store it as DATE_TIME you can use MySQL function TIMEDIFF. See http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff for more details.

poisson
  • 1,324
  • 11
  • 20