How to create a friendly date format (for example quot;submitted 2 days agoquot;)(如何创建友好的日期格式(例如“提交 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"
这篇关于如何创建友好的日期格式(例如“提交 2 天前")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何创建友好的日期格式(例如“提交 2 天前")


基础教程推荐
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01