创建类似于Facebook的时间戳?

时间:2013-08-12 18:54:17

标签: javascript jquery sql sql-server

很简单,2秒前,1天前,2周前,等等。最佳方法是什么?

使用SQL Server for DB。这是在查询级别完成的吗?或页面加载javascript?

此外,它如何经常自行更新...你认为客户端的所有javascript都是用户体验,还是很多ajax请求重新计算?

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

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

非常感谢你们。

4 个答案:

答案 0 :(得分:1)

您可以使用此SQL解决方案(sqlfiddle demo):

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
*/

答案 1 :(得分: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
    */

?>

答案 2 :(得分:0)

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

$('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
        // ...
    }
});

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

答案 3 :(得分:0)

- 另一种SQL方法

声明@datediffseconds int 声明@datetime datetime

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

选择 案件     当@datediffseconds&lt; 60 THEN CONVERT(varchar(20),(@ datediffseconds))+'秒前'     当@datediffseconds&lt; 3600 THEN CONVERT(varchar(20),(@ datediffseconds)/ 60)+'分钟前'     当@datediffseconds&lt; 86400 THEN CONVERT(varchar(20),(@ datediffseconds)/ 3600)+'小时前'     当@datediffseconds&lt; 604800 THEN CONVERT(varchar(20),(@ datediffseconds)/ 86400)+'days ago'     当@datediffseconds&lt; 31449600 THEN CONVERT(varchar(20),(@ datediffseconds)/ 604800)+'周前' END