我在 mysql 中有数据时间字段,我正在使用 php。 计算并向用户展示从当天到现场时间过去了多少时间的最佳方法是什么? 所以它呈现给我类似的东西:
2 days ago ..
2 weeks ago ...
最佳答案
您可以使用 SQL DATEDIFF函数在您的 SELECT 语句中包含已经过去的天数。然后在 php convert 中将天数转换为周或月...
类似于:
SELECT DATADIFF(date, NOW()) AS daysPassed, ... FROM table
在 php 中:
$show //the string to display
$days = ... //get the daysPassed value from SQL
if($days > 6)
$show = $days % 7 + " weeks ago";
else
$show = $days + " days ago";
关于mysql - 我如何计算从当前日期到 DateTime mysql 字段过去的天/周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8265900/