0

很简单,2 秒前、1 天前、2 周前等。完成此任务的最佳方法是什么?

为数据库使用 SQL Server。这是在查询级别完成的吗?还是页面加载时的javascript?

此外,它如何经常自行更新......你认为这都是客户端上的 JavaScript 以提供用户体验,还是大量的 ajax 请求重新计算?

只是寻找有关重现此类时间戳的最佳方法的建议。

我的网站上有一个评论系统,我只是使用表中的 SQL Date_Created 字段中的值。

非常感谢大家。

4

4 回答 4

1

您可以使用这个仅限 SQL 的解决方案(sqlfiddle 演示):

SELECT  ev.*,
        CASE 
            WHEN DATEDIFF(SECOND,ev.EventDate,GETDATE()) BETWEEN 0 AND 59
                THEN CONVERT(VARCHAR(25),DATEDIFF(SECOND,ev.EventDate,GETDATE())) + ' second' + CASE WHEN DATEDIFF(SECOND,ev.EventDate,GETDATE())>1 THEN 's' ELSE '' END
            WHEN DATEDIFF(MINUTE,ev.EventDate,GETDATE()) BETWEEN 0 AND 59
                THEN CONVERT(VARCHAR(25),DATEDIFF(MINUTE,ev.EventDate,GETDATE())) + ' minute' + CASE WHEN DATEDIFF(MINUTE,ev.EventDate,GETDATE())>1 THEN 's' ELSE '' END
            WHEN DATEDIFF(HOUR,ev.EventDate,GETDATE()) BETWEEN 0 AND 24
                THEN CONVERT(VARCHAR(25),DATEDIFF(HOUR,ev.EventDate,GETDATE())) + ' hour' + CASE WHEN DATEDIFF(HOUR,ev.EventDate,GETDATE())>1 THEN 's' ELSE '' END
            ELSE 
                CONVERT(VARCHAR(25),DATEDIFF(DAY,ev.EventDate,GETDATE())) + ' day' + CASE WHEN DATEDIFF(DAY,ev.EventDate,GETDATE())>1 THEN 's' ELSE '' END
        END AS Result
FROM    dbo.MyEvent ev;
GO
/*
EventID EventDate               Result
------- ----------------------- ---------
1       2013-08-12 22:20:03.323 5 seconds
2       2013-08-12 22:16:08.327 4 minutes
3       2013-08-12 19:20:08.327 3 hours
4       2013-08-09 22:20:08.330 3 days
5       2013-08-10 22:20:08.337 2 days
*/
于 2013-08-12T19:21:42.560 回答
0

我要做的是,在 JavaScript 中,查找页面上的所有时间戳并将它们与当前时间进行比较,然后处理该差异以输出您想要的内容。你说你正在使用jQuery,所以你可以做这样的事情。time假设您在元素中显示时间戳(顺便说一下,您可能应该这样做):

$('time').each(function () {
    // Gets timestamp time and current time in milliseconds
    var time = Date.parse($(this).attr('datatime')),
        currentTime = new Date().getTime(),
        difference = (currentTime - time) / 1000;

    // Use the difference to update the timestamp value accordingly
    if (difference < 60) {
        $(this).val(difference + ' seconds');
    } else if (difference > 60 && difference < 3600) {
        $(this).val(difference / 60 + ' minutes');
    } else if (difference > 3600 && difference < 86400) {
        // You get the idea
        // ...
    }
});

这样,您就可以避免每次都对服务器进行不必要且非常昂贵的调用来更新它们。您只需首先检索它们一次,然后让用户的浏览器自行更新它们。

于 2013-08-12T19:11:05.520 回答
0

这是我认为您可能正在寻找的确切内容的链接!

http://snipplr.com/view/37578/

<?php
    function nicetime($date) {
        if(empty($date)) {
            return "No date provided";
        }

        $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
        $lengths = array("60","60","24","7","4.35","12","10");

        $now = time();
        $unix_date = strtotime($date);

        // check validity of date
        if(empty($unix_date)) return "Bad date";

        // is it future date or past date
        if($now > $unix_date) {    
            $difference = $now - $unix_date;
            $tense = "ago";
        }
        else {
            $difference = $unix_date - $now;
            $tense = "from now";
        }

        for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) $difference /= $lengths[$j];

        $difference = round($difference);

        if($difference != 1) $periods[$j].= "s";

        return "$difference $periods[$j] {$tense}";
    }

    /*
    $date = "2009-03-04 17:45";
    $result = nicetime($date); // 2 days ago
    */

?>
于 2013-08-12T19:02:53.837 回答
0

--另一种SQL方法

声明@datediffseconds int 声明@datetime datetime

SELECT @datetime = '2013-06-11 14:39:58.947' SELECT @datediffseconds = DATEDIFF(SECOND,@datetime, GETDATE())

SELECT CASE WHEN @datediffseconds < 60 THEN CONVERT(varchar(20),(@datediffseconds)) + ' seconds ago' WHEN @datediffseconds < 3600 THEN CONVERT(varchar(20),(@datediffseconds)/60) + 'minutes ago'当@datediffseconds < 86400 THEN CONVERT(varchar(20),(@datediffseconds)/3600) + '几小时前' 当@datediffseconds < 604800 THEN CONVERT(varchar(20),(@datediffseconds)/86400) + '天前'当@datediffseconds < 31449600 THEN CONVERT(varchar(20),(@datediffseconds)/604800) + '数周前' END

于 2013-08-12T20:00:43.413 回答