让 PHP/MySQL 时间戳看起来更有吸引力
基本上,我当前正在从 MySQL 数据库中选择时间戳。在数据库中,时间戳看起来像这样:
2010-06-30 12:36:08
显然对于 Web 应用程序来说,这对于用户查看来说不太有吸引力。因此,使用一些 CodeIgniter 函数,我让它看起来更好一些。
<h4 class="timestamp">
<?php // Quickly calculate the timespan
$post_date = mysql_to_unix($row->date);
$now = time();
echo timespan($post_date, $now);?> ago
</h4>
如果您不使用 CodeIgniter,那么除了 echo timespan()
之外,一切都是标准 PHP。 CodeIgniter 只是将其回应为“英语”时间跨度。因此,示例输出如下:
2 Months, 4 Weeks, 5 Hours, 20 Minutes ago
这一切都很好,但是,我想让它看起来更好......有太多逗号,有点太长了(我知道,我很挑剔......)。我喜欢是:
- 如果时间跨度小于一天,则输出应为
7小时33分钟前
- 如果时间跨度小于一周,输出应为
4 天前
- 如果时间跨度小于一个月,则输出应为
2 周,6 天前
- 如果时间跨度超过一个月,输出应为
4 个月前
- 在时间跨度超过一年的最终情况下,输出应为
一年多前
如您所见,我'我目前使用 CodeIgniter 函数来简化此操作 - 但如果有任何本机 PHP 函数可以完成我想要的操作,那就太棒了。
感谢您的帮助!
杰克
So basically, I'm currently selecting a Timestamp from my MySQL database. In the database, the timestamp looks like so:
2010-06-30 12:36:08
Obviously for a webapp, that's not very attractive for users to view. So, using some CodeIgniter functions, I made it look a bit nicer.
<h4 class="timestamp">
<?php // Quickly calculate the timespan
$post_date = mysql_to_unix($row->date);
$now = time();
echo timespan($post_date, $now);?> ago
</h4>
If you don't do CodeIgniter, everything's standard PHP except for echo timespan()
. CodeIgniter just echoes it as an 'English' timespan. So, an example output would be:
2 Months, 4 Weeks, 5 Hours, 20 Minutes ago
That's all well and good, but, I want to make it seem nicer still... there are too many commas and its all a tad too long (I know, I'm picky...). What I'd like is:
- If the timespan is less than a day old, the output should be
7 hours, 33 minutes ago
- If the timespan is less than a week old, the output should be
4 days ago
- If the timespan is less than a month old, the output should be
2 weeks, 6 days ago
- If the timespan is over a month old, the output should be
4 months ago
- In the eventual case of the timespan being over a year old, the output should be
Over a year ago
As you can see, I'm currently using a CodeIgniter function to simplify this - but if there's any native PHP function that can do what I want it to, that'll be awesome.
Thanks for your help!
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这最好在客户端的表示层中完成。这是一个JS解决方案:
Timeago是一个jQuery插件,可以轻松支持自动更新模糊时间戳(例如“4 分钟前”或“大约 1 天前”)。
Timeago 会将
title
中具有 timeagoclass
和 ISO 8601 时间戳的所有 abbr 元素转换为如下所示:
将日期转换为 ISO 8601格式你可以这样做:
示例:
This is best done on the client side in the presentation layer. Here is a JS solution:
Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
Timeago will turn all abbr elements with a
class
of timeago and an ISO 8601 timestamp in thetitle
:into something like this:
To convert the date into the ISO 8601 format you can do something like this:
Examples:
如果您不想要“1 天”之类的内容,则必须进行一些调整。
You'll have to make a few adjustments if you don't want stuff like "1 days".