如何创建友好的日期格式(例如“提交 2 天前")
可能重复:
如何计算相对时间?
我想在我的社交网络应用程序上设置日期格式,就像 Digg.com 和其他网站一样.在那里,您会看到非常友好的日期,例如:
I want to format dates on my social web app much like Digg.com and other sites do. There, you see very friendly dates, such as:
- 刚刚
- 3 分钟前
- 一小时前
- 2 周前
- 6 个月前
- 等
在我开始创建这样一个东西之前,有没有人知道任何现成的脚本,我只需插入一个日期戳,并根据它与当前时间的关系给出一个友好的日期文本?
Before I wrap my head around creating such a thing, does anyone know of any ready-to-go script for this where I simply insert a datestamp and a friendly date text is given based on how it related to the current time?
PS:我在 PHP 中需要这个,但伪代码或任何其他语言也可以.
PS: I need this in PHP, but pseudo-code or any other language is fine too.
推荐答案
这是这个 问题.除了这个站点使用的代码之外,它还有一系列关于如何完成此操作的代码示例.我看了一眼,那里似乎也发布了一个 PHP 实现.
This is a duplicate of this question. It has a flurry of code samples on how to accomplish this, in addition to the code this very site uses. I glanced at it and there seems to be a PHP implementation posted there too.
除此之外,如果你使用 jQuery,你可以在客户端使用类似 timeago 插件.它的优点是随着时间的推移更新文本,所以如果你加载一个页面并显示5 分钟前发布"并在 5 分钟后再次查看,它会显示10 分钟前发布"
In addition to all this, if are you using jQuery you can do this client-side with something like the timeago plugin. It has the advantage of updating the text as time passes so if you load a page and it says "posted 5 minutes ago" and look again 5 minutes later, it says "posted 10 minutes ago"
相关文章