如何显示更新后的时间像脸书和推特
How to show post update time like facebook and twitter?
我正在制作一个通知牌。而不是显示通知到来的时间。我想显示通知到达后的时间。正如它发生在推特和脸书上,他们显示3200万,1小时前等
我想实现这个代码是JavaScript/Java。
任何帮助都将不胜感激。
查看jQuery的PrettyDate插件,由John Resig自己
http://ejohn.org/blog/javascript-pretty-date/
从过帐时间中减去当前时间。剩余的毫秒数可以用来格式化一个漂亮的"。。。ago'字符串。
一些javascript时间函数可以在这里找到。我发现的一些算法(php):
function TimeAgo($datefrom,$dateto=-1)
{
// Defaults and assume if 0 is passed in that
// its an error rather than the epoch
if($datefrom<=0) {
return "A long time ago";
}
if($dateto==-1) {
$dateto = time();
}
// Calculate the difference in seconds betweeen
// the two timestamps
$difference = $dateto - $datefrom;
// If difference is less than 60 seconds,
// seconds is a good interval of choice
if($difference < 60)
{
$interval = "s";
}
// If difference is between 60 seconds and
// 60 minutes, minutes is a good interval
elseif($difference >= 60 && $difference<60*60)
{
$interval = "n";
}
// If difference is between 1 hour and 24 hours
// hours is a good interval
elseif($difference >= 60*60 && $difference<60*60*24)
{
$interval = "h";
}
// If difference is between 1 day and 7 days
// days is a good interval
elseif($difference >= 60*60*24 && $difference<60*60*24*7)
{
$interval = "d";
}
// If difference is between 1 week and 30 days
// weeks is a good interval
elseif($difference >= 60*60*24*7 && $difference <
60*60*24*30)
{
$interval = "ww";
}
// If difference is between 30 days and 365 days
// months is a good interval, again, the same thing
// applies, if the 29th February happens to exist
// between your 2 dates, the function will return
// the 'incorrect' value for a day
elseif($difference >= 60*60*24*30 && $difference <
60*60*24*365)
{
$interval = "m";
}
// If difference is greater than or equal to 365
// days, return year. This will be incorrect if
// for example, you call the function on the 28th April
// 2008 passing in 29th April 2007. It will return
// 1 year ago when in actual fact (yawn!) not quite
// a year has gone by
elseif($difference >= 60*60*24*365)
{
$interval = "y";
}
// Based on the interval, determine the
// number of units between the two dates
// From this point on, you would be hard
// pushed telling the difference between
// this function and DateDiff. If the $datediff
// returned is 1, be sure to return the singular
// of the unit, e.g. 'day' rather 'days'
switch($interval)
{
case "m":
$months_difference = floor($difference / 60 / 60 / 24 /
29);
while (mktime(date("H", $datefrom), date("i", $datefrom),
date("s", $datefrom), date("n", $datefrom)+($months_difference),
date("j", $dateto), date("Y", $datefrom)) < $dateto)
{
$months_difference++;
}
$datediff = $months_difference;
// We need this in here because it is possible
// to have an 'm' interval and a months
// difference of 12 because we are using 29 days
// in a month
if($datediff==12)
{
$datediff--;
}
$res = ($datediff==1) ? "$datediff month ago" : "$datediff
months ago";
break;
case "y":
$datediff = floor($difference / 60 / 60 / 24 / 365);
$res = ($datediff==1) ? "$datediff year ago" : "$datediff
years ago";
break;
case "d":
$datediff = floor($difference / 60 / 60 / 24);
$res = ($datediff==1) ? "$datediff day ago" : "$datediff
days ago";
break;
case "ww":
$datediff = floor($difference / 60 / 60 / 24 / 7);
$res = ($datediff==1) ? "$datediff week ago" : "$datediff
weeks ago";
break;
case "h":
$datediff = floor($difference / 60 / 60);
$res = ($datediff==1) ? "$datediff hour ago" : "$datediff
hours ago";
break;
case "n":
$datediff = floor($difference / 60);
$res = ($datediff==1) ? "$datediff minute ago" :
"$datediff minutes ago";
break;
case "s":
$datediff = $difference;
$res = ($datediff==1) ? "$datediff second ago" :
"$datediff seconds ago";
break;
}
return $res;
}
相关文章:
- 谷歌表格注释时间线显示精确的值
- 如何设置此javascript以正确显示花费的总时间,显示超出正常时钟的分钟数
- 谷歌可视化时间线显示鼠标光标的时间
- 使用当前时间同步显示
- Javascript 倒计时时间无法显示
- DHTML - 在有限的时间内显示图片
- NodeJS时间戳:比较时间并显示记录排序升序
- 获取 d3 以根据时间戳显示正确的日期
- 检测YouTube时间然后显示DIV?使用 JavaScript
- 如何在页面加载时隐藏图像,然后在一段时间后显示滑动
- 使用时间戳显示活动用户
- 在比较时间时显示警报消息
- 按照客户端日期时间格式显示日期
- 基于时间突出显示行
- 在设定时间后显示跨度类
- 鼠标悬停需要时间来显示图像
- 引导模式有时显示和其他时间不显示
- 为什么Jquery在使用设置分钟功能设置时间后显示当前系统时间
- 时间不显示
- 格式化对象键到月份,按时间顺序显示每个月份