如何创建友好的日期格式(例如“2天前提交”) [英] How to create a friendly date format (for example "submitted 2 days ago")

查看:156
本文介绍了如何创建友好的日期格式(例如“2天前提交”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何计算相对时间?

I想要在我的社交网络应用程序上格式化日期,就像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分钟前

  • 1小时前

  • 2周前

  • 6个月前

  • etc

  • just now
  • 3 minutes ago
  • one hour ago
  • 2 weeks ago
  • 6 months ago
  • etc

在我围绕创建这样的事情之前,有没有人知道任何即时的脚本在那里我简单地插入一个日期戳,一个友好的日期文本是基于它如何与当前时间相关的?

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"

这篇关于如何创建友好的日期格式(例如“2天前提交”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆