douren1891 2012-08-13 20:35
浏览 39
已采纳

PHP:格式化时间Stackoverflow或Apple Mail样式

There is this really nice function from the php.net documentation that enables you to format time in a Facebook-style manner (e.g., 2 minutes ago, 4 weeks ago, or 3 years ago).

However, I prefer the way Stackoverflow and Apple Mail does it which is generally as follows:

  1. The current day is listed in x seconds ago or x hours ago or time (e.g, 4:35pm).
  2. Yesterday is listed as "Yesterday".
  3. All days after that are listed by M/D/Y.

Has anyone adapted this php.net script to do this or might share a different script that accomplishes the same goal?

<?php 

  function nicetime($date)
 {
    if(empty($date)) {
      return "No date provided";
    }

$periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths         = array("60","60","24","7","4.35","12","10");

$now             = time();
$unix_date         = strtotime($date);

   // check validity of date
if(empty($unix_date)) {    
    return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {    
    $difference     = $now - $unix_date;
    $tense         = "ago";

} else {
    $difference     = $unix_date - $now;
    $tense         = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
    $difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
    $periods[$j].= "s";
}

  return "$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>
  • 写回答

1条回答 默认 最新

  • douhan9748 2012-08-13 23:22
    关注

    Ok, i answered my own question.

    The key is to track how many rounds of division the for loop goes through until the quotient of the current time minus the input time, $difference, divided by $jth value of the $lengths array item is less than the $j+1th value of this array.

    I track this by incrementing the variable $i (notice the if/elseif/else clause demonstrating each of the 3 points I mention above) in this modified version of nicetime():

         ///http://php.net/manual/en/function.time.php
      function nicetime($date)
     {
        if(empty($date)) {
          return "No date provided";
        }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }
    
    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
    
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    $i=0;
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $i++;
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    if($i<3){
        $day="$difference $periods[$j] {$tense}";
        return $day;
        //satisfies case #1 where time is listed as seconds, minutes, hours ago
    }
    
    elseif($i==3){
        $difference == 1 && $periods[$j]=='day' ? $day='yesterday':     
        $day="$difference $periods[$j] {$tense}";
        return $day;        
        //satisfies case #2 where time is listed as yesterday if not the current day
    
    }
    else{
        return $date;       
        // satisfies case #3 where date is listed as M/D/Y if greater than a week old
    }   
    
    }
    
    echo "case#1: ".nicetime('2012-08-13 23:12:16');
    echo "case#2: ".nicetime('2012-08-12 23:12:16');
    echo "case#3: ".nicetime('2012-07-07 23:12:16');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器