mysql - 我如何计算从当前日期到 DateTime mysql 字段过去的天/周

标签 mysql datetime

我在 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/

相关文章:

c# - 如何在日期时间的数据列中找到最大值?

python - 从 Python 中的 float 中获取日期时间

mysql - 更新时间间隔内第一次出现的值

python - Yosemite 上的 Python 2.7.6 MySQLdb 问题

mysql select 抛出错误为“无法写入”;表中的重复键

mysql - 尝试创建一个 Access 2013 查询,该查询选择上一年 9 月 1 日到当年 8 月 31 日之间的日期

python - 将 dask 列转换为日期并应用 lambda 函数

PHP: 日期时间 '-1 day'

mysql - 毫秒还是日期类型?

.net - Commit 期间 MySQL 超时