让 PHP/MySQL 时间戳看起来更有吸引力

发布于 2024-09-07 16:03:57 字数 1005 浏览 3 评论 0原文

基本上,我当前正在从 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

这一切都很好,但是,我想让它看起来更好......有太多逗号,有点太长了(我知道,我很挑剔......)。我喜欢是:

  1. 如果时间跨度小于一天,则输出应为7小时33分钟前
  2. 如果时间跨度小于一周,输出应为 4 天前
  3. 如果时间跨度小于一个月,则输出应为 2 周,6 天前
  4. 如果时间跨度超过一个月,输出应为 4 个月前
  5. 在时间跨度超过一年的最终情况下,输出应为 一年多前

如您所见,我'我目前使用 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:

  1. If the timespan is less than a day old, the output should be 7 hours, 33 minutes ago
  2. If the timespan is less than a week old, the output should be 4 days ago
  3. If the timespan is less than a month old, the output should be 2 weeks, 6 days ago
  4. If the timespan is over a month old, the output should be 4 months ago
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-09-14 16:03:57

这最好在客户端的表示层中完成。这是一个JS解决方案:

Timeago是一个jQuery插件,可以轻松支持自动更新模糊时间戳(例如“4 分钟前”或“大约 1 天前”)。

Timeago 会将 title 中具有 timeago class 和 ISO 8601 时间戳的所有 abbr 元素转换

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

为如下所示:

<abbr class="timeago" title="July 17, 2008">about a year ago</abbr>

将日期转换为 ISO 8601格式你可以这样做:

<?= date("c", $post_date) ?>

示例:

您打开此页面不到
分钟前。 (这将更新每个
分钟。等等。)

此页面最后修改时间为 11 天
以前。

Ryan 出生于 31 年前。

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 the title:

<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>

into something like this:

<abbr class="timeago" title="July 17, 2008">about a year ago</abbr>

To convert the date into the ISO 8601 format you can do something like this:

<?= date("c", $post_date) ?>

Examples:

You opened this page less than a
minute ago. (This will update every
minute. Wait for it.)

This page was last modified 11 days
ago.

Ryan was born 31 years ago.

忆伤 2024-09-14 16:03:57
$ts = new DateTime();
$ts->setTimestamp($my_timestamp);
$cur = new DateTime();
$difference = $cur->diff($ts);
if ($difference->format("%a") == 0)
    $out = $difference->format("%h hours %i minutes");
elseif ($difference->format("%a") < 7)
    $out = $difference->format("%a days");
elseif ($difference->format("%m") == 0) {
    $days = $difference->format("%a");
    $out = sprintf("%d weeks %d days", floor($days / 7),
        $days % 7);
}
elseif ($difference->format("%y") == 0)
    $out = $difference->format("%m months");
else
    $out = "over a year";

如果您不想要“1 天”之类的内容,则必须进行一些调整。

$ts = new DateTime();
$ts->setTimestamp($my_timestamp);
$cur = new DateTime();
$difference = $cur->diff($ts);
if ($difference->format("%a") == 0)
    $out = $difference->format("%h hours %i minutes");
elseif ($difference->format("%a") < 7)
    $out = $difference->format("%a days");
elseif ($difference->format("%m") == 0) {
    $days = $difference->format("%a");
    $out = sprintf("%d weeks %d days", floor($days / 7),
        $days % 7);
}
elseif ($difference->format("%y") == 0)
    $out = $difference->format("%m months");
else
    $out = "over a year";

You'll have to make a few adjustments if you don't want stuff like "1 days".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文