doujiu9172 2013-05-23 11:04
浏览 37
已采纳

使用php获取正常的用户可读日期/时间

I know this question has been asked 2-3 times before, and I am using a function which I found in one of the answers(check it here) from those questions, but it doesnt seem to be working fine for me, i asked there in comments but didnt get any reply, so I am opening a new question here. following is the code to get plain text date/time from a datetime(mysql type).

function getElapsedTime($time_stamp)
{
    $ts = convert_datetime($time_stamp);
    $time_stamp = $ts;
    $time_difference = strtotime('now') - $time_stamp;

    if ($time_difference >= 60 * 60 * 24 * 365.242199)
    {
        return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
    }
    elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
    {
       return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
    }
    elseif ($time_difference >= 60 * 60 * 24 * 7)
    {
        return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
    }
    elseif ($time_difference >= 60 * 60 * 24)
    {
        return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
    }
    elseif ($time_difference >= 60 * 60)
    {
        return get_time_ago_string($time_stamp, 60 * 60, 'hour');
    }
    elseif($time_difference <= 60)
    {
        return get_time_ago_string($time_stamp, 60, 'minute');
    }
}

function get_time_ago_string($time_stamp, $divisor, $time_unit)
{
    $time_difference = strtotime("now") - $time_stamp;
    $time_units      = floor($time_difference / $divisor);

    settype($time_units, 'string');

    if ($time_units === '0')
    {
        return 'less than 1 ' . $time_unit . ' ago';
    }
    elseif ($time_units === '1')
    {
        return '1 ' . $time_unit . ' ago';
    }
    else
    {
       return $time_units . ' ' . $time_unit . 's ago';
    }
}

function convert_datetime($str) { 

   list($date, $time) = explode(' ', $str); 
   list($year, $month, $day) = explode('-', $date); 
   list($hour, $minute, $second) = explode(':', $time); 

   $timestamp = mktime($hour, $minute, $second, $month, $day, $year); 

   return $timestamp; 
}  

I have added one more function convert_datetime() which turns mysql datetime date into timestamp, as I thought the function getElapsedTime() is expecting timestamp, because when I was passing normal datetime date(mysql format), it wasn't working. So now, I am passing mysql datetime to getElapsedTime() and converting it into timestamp using convert_datetime() function and using it to get string.

But when the date is expected to return for "minute" or "second", its returning in negative value. eg its returning "-182 minutes ago" for "2013-05-23 15:59:58"..

Anybody knows how to get it done?

  • 写回答

2条回答 默认 最新

  • doujiao1981 2013-05-23 11:09
    关注

    // try this function

    function time_ago($ptime) {
      $ptime = strtotime($ptime);
      if ($ptime) {
        $etime = time() - $ptime;
        if ($etime < 1)
          return 'few seconds';
        $a = array(
            12 * 30 * 24 * 60 * 60 => 'year',
            30 * 24 * 60 * 60 => 'month',
            24 * 60 * 60 => 'day',
            60 * 60 => 'hour',
            60 => 'minute',
            1 => 'second'
        );
        foreach ($a as $secs => $str) {
          $d = $etime / $secs;
          if ($d >= 1) {
            $r = round($d);
            return $r . ' ' . $str . ($r > 1 ? 's' : '') . '';
          }
        }
      }
      return '0';
    }
    

    // usage

    echo time_ago('2013-05-23 15:59:58');
    
    echo time_ago(date('Y:m:d H:i:s'));
    

    // Output

    43 minutes
    
    few seconds
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名