【问题标题】:function that formats the time difference格式化时差的函数
【发布时间】:2011-07-17 15:52:51
【问题描述】:

我正在尝试创建一个函数,将日期与当前时间进行比较,并返回一个格式良好的字符串。
我匆忙编写了一些代码并且它可以工作,但我正在尝试找到一种更有效的方法。这是我的代码:

    function _formatDate($dateStr)
    {
    $timestr = "";
    $t= time() - strtotime($dateStr);
    if($t < 60) {
        $timestr = "{$t} seconds ago";
    }
    elseif ($t <120) {
        $timestr = "about a minute ago";
    }
    elseif ($t < 3600) {
        $minute = floor($t/60);
        $timestr = "{$minute} minutes ago";
    }
    elseif ($t < 7200) {
        $timestr = " about an hour ago";
    }
    elseif ($t < 86400) {
        $hour = floor($t/3600);
        $timestr = "{$hour} hours ago";
    }
    elseif ($t < 172800) {
        $timestr = "a day ago";
    }
    elseif ($t < 2592000) {
        $day = floor($t/86400);
        $timestr = "{$day} days ago";
    }
    elseif ($t < 5184000){
        $timestr = "about a month ago";
    }
    else {
        $month = floor($t/2592000);
        $timestr = "{$month} months ago";
    }
    return $timestr;
}

【问题讨论】:

  • 我正在尝试将日期作为参数传递并让它自动返回,例如:22 分钟前或 3 天前
  • 那么你的代码有什么问题?似乎按照你的要求做。如果这是用于网页,则输出为日期/时间戳,然后使用 JS(和 TimeAgo 之类的库)来更新值可能是有益的。
  • 我想知道是否有更有效的方法来做到这一点,没有那么多 if else 语句。
  • 高效很难定义... 更少的行 - 当然。更容易理解 - 有问题。需要较少的处理 - 不太可能。

标签: php function time


【解决方案1】:

我建议在 javascript 中执行此操作。您可以使用 jquery 的 TimeAgo 插件。很酷的一点是它将在客户端实时更新,而无需任何页面刷新。这使用起来非常简单,并且符合您的要求。

【讨论】:

    【解决方案2】:

    我使用的一些代码从未失败,只需将 Unix 时间戳放入,如果您在函数中使用可以作为“to”条件的第二个参数

    echo timeDiffrence('1300392875');
    
    function timeDiffrence($from, $to = null){
        $to = (($to === null) ? (time()) : ($to));
        $to = ((is_int($to)) ? ($to) : (strtotime($to)));
        $from = ((is_int($from)) ? ($from) : (strtotime($from)));
        $units = array
        (
        "y"   => 29030400, // seconds in a year   (12 months)
        "month"  => 2419200,  // seconds in a month  (4 weeks)
        "w"   => 604800,   // seconds in a week   (7 days)
        "d"    => 86400,    // seconds in a day    (24 hours)
        "h"   => 3600,     // seconds in an hour  (60 minutes)
        "m" => 60,       // seconds in a minute (60 seconds)
        "s" => 1         // 1 second
        );
        $diff = abs($from - $to);
        $suffix = (($from > $to) ? ("from now") : ("ago"));
        foreach($units as $unit => $mult)
        if($diff >= $mult)
        {
            //$and = (($mult != 1) ? ("") : ("and "));
            $output .= "".$and.intval($diff / $mult)."".$unit.((intval($diff / $mult) == 1) ? ("") : (""));
            $diff -= intval($diff / $mult) * $mult;
        }
        $output .= " ".$suffix;
        $output = substr($output, strlen(""));
        if($output =='go' || $output ==' ago'){$output = 'A few secs ago';}
        return $output;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 2020-10-13
      • 2016-05-19
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      相关资源
      最近更新 更多