显示几天前而不是日期

时间:2012-11-05 18:13:01

标签: php mysql date-format

我的网站上有一个评论区域,每个评论都以正常日期格式(2012年11月5日)发布...但我想显示'1天前'甚至'3小时'以前'而不是正常的格式...这是我到目前为止所拥有的:

SELECT p.poll_id, p.title, date_format(c.posted, '%D %b %y') as time, COUNT( c.poll_id ) AS count, u.username
FROM users AS u
JOIN polls AS p ON u.user_id = p.user_id
LEFT JOIN comment AS c ON p.poll_id = c.poll_id
LIMIT 5 

1 个答案:

答案 0 :(得分:2)

使用date difference function评估日期差异

SELECT p.poll_id, p.title,  DATEDIFF(NOW(), c.posted) AS daysold, COUNT( c.poll_id ) AS   count, u.username
  FROM users AS u
  JOIN polls AS p ON u.user_id = p.user_id
  LEFT JOIN comment AS c ON p.poll_id = c.poll_id
  LIMIT 5