需要重写日期显示功能 [英] Need to rewrite function for date display

查看:124
本文介绍了需要重写日期显示功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几年前写过的以下功能。它需要我的数据库中的datetime,并以更好的格式化方式显示它。

I have the following function that I wrote couple of years ago. It takes a datetime from my db and displays it in a better formatted way.

function formatTime($dateTime){
// show time only if posted today
if (date('Ymd') == date('Ymd', strtotime($dateTime))) {
    $dt = date('g:i a', strtotime($dateTime));
} else {
    // if not the same year show YEAR 
    if (date('Y') == date('Y', strtotime($dateTime))) {
        $dt = date('M j', strtotime($dateTime));
    } else {
        $dt = date('M j, Y', strtotime($dateTime));
    }
}

return $dt;
}

我使用服务器时间,这是CST。昨天我有一个来自澳大利亚的用户指出,对于他来说,自从他在一个完全不同的时区,实际上是在一天的时间(相对于我在某个时间的输出),他并没有做任何事。

I use server time, which is CST for me. Yesterday I had a user from Australia pointing out that for him it did not make any since since he way on an entirely different time zone, actually a day ahead (when compared to my output at certain time :).

我决定重写我的功能,说如下:

I decided to rewrite my function to say something like:


  • 如果在一分钟以前

  • 如果在一小时之内>#分钟前

  • 1小时以前>一个多小时前

  • 2 - 24小时>天前

  • 2 - 7天>#天前

  • 7天 - 月>#周前

  • 1 - 2个月>一个月以上

  • 之后,我可以显示一个日期

  • if under a minute > seconds ago
  • if under an hour > # minutes ago
  • between 1 -2 hrs > over an hour ago
  • 2 - 24 hrs > day ago
  • 2 - 7 days > # days ago
  • 7 days - month > # weeks ago
  • 1 - 2 months > over a month
  • after that I can just show a date

有没有什么功能,你可能会意识到这样做,如果不是我如何修改这个?

Are there any functions that you are perhaps aware of doing this, if not how would I modify this one?

谢谢。

推荐答案

function formatTime ($dateTime) {

  // A Unix timestamp will definitely be required
  $dateTimeInt = strtotime($dateTime);

  // First we need to get the number of seconds ago this was
  $secondsAgo = time() - $dateTimeInt;

  // Now we decide what to do with it
  switch (TRUE) {

    case $secondsAgo < 60: // Less than a minute
      return "$secondsAgo seconds ago";

    case $secondsAgo < 3600: // Less than an hour
      return floor($secondsAgo / 60)." minutes ago";

    case $secondsAgo < 7200: // Less than 2 hours
      return "over an hour ago";

    case $secondsAgo < 86400: // Less than 1 day
      return "1 day ago"; // This makes no sense, but it is what you have asked for...

    case $secondsAgo < (86400 * 7): // Less than 1 week
      return floor($secondsAgo / 86400)." days ago";

    case $secondsAgo < (86400 * 28): // Less than 1 month - for the sake of argument let's call a month 28 days
      return floor($secondsAgo / (86400 * 7))." weeks ago";

    case $secondsAgo < (86400 * 56): // Less than 2 months
      return "over a month ago";

    default:
      return date('M j, Y', $dateTimeInt);

  }

}

没有任何意义上的完美无缺,特别是因为你的一个要求没有任何意义(见评论),但希望它能给你一个正确的方向,并说明如何使用开关允许您轻松地从行为中添加和删除项目/选项。

This is by no means flawless, especially since one of your requirements doesn't make sense (see comments) but hopefully it should give you a push in the right direction, and illustrate how you can use switch to allow you to easily add and remove items/options from the behaviour.

这篇关于需要重写日期显示功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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