【问题标题】:How to get timestamp of MySQL table in relative time on website如何在网站上以相对时间获取 MySQL 表的时间戳
【发布时间】:2013-06-24 11:14:19
【问题描述】:

我想逐步了解如何将我的 mysql 表 中的 时间戳 转换为相对时间(4 秒前,5 个月以前...)在我的网站上。

关于信息,我的时间戳列名为creation,我的表名为users

我试过的代码:

function relativedate($secs) {
    $second = 1;
    $minute = 60;
    $hour = 60*60;
    $day = 60*60*24;
    $week = 60*60*24*7;
    $month = 60*60*24*7*30;
    $year = 60*60*24*7*30*365;

    if ($secs <= 0) { $output = "now";
    }elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
    }elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
    }elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
    }elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
    }elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
    }elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
    }elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
    }else{ $output = " more than a decade ago"; }

    if ($output <> "now"){
        $output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
    }
    return $output;
}



echo relativedate(60); // 1 minute

【问题讨论】:

  • 我找到了多个代码,但它不起作用...我只是复制/粘贴,我不知道如何集成它们...
  • 这段代码有什么问题?您从 mysql 获取时间的格式是什么?
  • «echo relativedate(60);»只显示 «1 分钟»...它卡在那里。
  • 你期待什么?
  • 我想获取数据库中的时间戳,并以 «3 分钟前...» 等格式显示它

标签: php mysql time timestamp relative-date


【解决方案1】:

使用 CASE 显示相对时间/日期。 我在这段代码中创建了表“样本”。

SELECT *, CASE
WHEN creation between date_sub(now(), INTERVAL 60 minute) and now() THEN concat(minute(TIMEDIFF(now(), creation)), ' minutes ago')
WHEN datediff(now(), creation) = 1 THEN 'Yesterday'
WHEN creation between date_sub(now(), INTERVAL 24 hour) and now() THEN concat(hour(TIMEDIFF(NOW(), creation)), ' hours ago')
ELSE date_format(creation, '%a, %m/%d/%y')
END as date FROM sample 

这将创建一个新列“日期”,您可以使用它来输出相对时间。

【讨论】:

  • 您应该将其包含在您的查询中。您可能在那里使用 SELECT * FROM sample 之类的东西,将其更改为包含 CASE 子句并输出转换后的行(日期,在我的示例中)。
  • 在你的 mysql 查询中。您只需要添加 CASE 子句而不是 'creation' 列。这会将日期更改为相对时间。
  • 要在您的网站上显示数据,您需要创建一个查询 (mysqli_query),然后使用 fetch (mysqli_fetch_array) 输出数据。您可以修改查询的输出,而不是使用 SELECT * FROM table 来显示所有数据。这将修改 SQL 输出,因此您无需使用 PHP 函数进行修改。尝试在您的代码中查找查询语句并发布内容。
  • 但是对于更改创建列,您是否有一段代码要给我,以添加到我在第一篇文章中提供的代码中?我实际上迷失了你所有的答案......但感谢你的时间!
【解决方案2】:

您可以通过 PHP 执行您的请求(称为“时间前”)。

  • 这是github上的一个PHP插件,php-time-ago
  • 这里是 a relevant SO question - 试试 time_elapsed_string()time_ago() 函数
  • 还有一个jQuery插件,如果你想在客户端做转换,timeago

【讨论】:

  • 感谢您的回答。我还有2个问题。我已经尝试过github的第一个例子。首先,它给了我这个错误:找不到类'TimeAgo'......另外,我怎样才能把我的数据库的日期放在他们在例子中给出的日期?
  • 您的代码中是否包含了 timeago.inc.php?至于你的日期......它是 dateDifference() 函数的第二个参数,在快速检查代码后
【解决方案3】:

我喜欢 Jm Verastigue 并对其进行了进一步的调整,以增强当年内的日期与当年外的日期,并且也只适用于 utc_time,但 now() 方法也有效。

SELECT 
report_time,
convert(CASE
    WHEN
        report_time between date_sub(utc_timestamp(),
            INTERVAL 60 minute) and utc_timestamp()
    THEN
        concat(minute(TIMEDIFF(utc_timestamp(), report_time)),
                ' minutes ago')
    WHEN datediff(utc_timestamp(), report_time) = 1 THEN 'Yesterday'
    WHEN
        report_time between date_sub(utc_timestamp(), INTERVAL 24 hour) and utc_timestamp()
    THEN
        concat(hour(TIMEDIFF(utc_timestamp(), report_time)),
                ' hours ago')
    WHEN
        year(report_time) >= year(utc_timestamp())
    THEN
        date_format(report_time, '%e %b')
    ELSE date_format(report_time, '%Y-%m-%d')
END using utf8) as `LekkerDate`

FROM
        _smry_prodinfo
    group by `LekkerDate`;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 2022-01-15
    • 1970-01-01
    • 2011-08-31
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多