【问题标题】:show time like " 2 minutes ago"显示“2 分钟前”之类的时间
【发布时间】:2016-02-26 16:42:45
【问题描述】:

我是yii2的初学者。我想显示帖子创建的时间。我使用 bellow 函数来获取它,但结果只是 0 分钟前。谁能帮帮我?

<?php
function notifyDate($myStartDate) {
  $now = Yii::$app->jdate->date('Y/m/d') . '- ' . date('H:i:s');

  $datediff = $now - $myStartDate;
  if ($datediff < (60 * 60)) {  // Minutes
      return floor($datediff / (60 * 60 * 24)) . " Minutes ago ";
  }
  if ($datediff < (60 * 60 * 24)) {  // Hours
      return floor($datediff / (60 * 60 * 24)) . " Hours ago ";
  }
  // this  return the number of day
  return floor($datediff / (60 * 60 * 24));
}
?>

<?php
  $last_comment = Comment::find()->orderBy(['id' => SORT_DESC])->one();
  $myStartDate = $last_comment['created_time'];
  $now = Yii::$app->jdate->date('Y/m/d') . '-' . date('H:i:s');
?>

<span class="pull-right text-muted small">
    <em><?php echo notifyDate($myStartDate); ?></em>
</span>

【问题讨论】:

  • 检查 last_comment['created_time'] 中是否有正确的值; ,, 试试 var:dump(last_comment['created_time'];);
  • 我使用相同的代码提交 created_time。它的波纹管代码。 $model->created_time = Yii::$app->jdate->date('Y/m/d') 。 '-' 。日期('H:i:s');
  • 您是否尝试过检查过帐值的内容?结果如何?
  • 数据库中created_time为:1394/12/05-18:51:15,$now值为:1394/12/07-21:31:22

标签: php yii yii2-advanced-app


【解决方案1】:

为了更优雅的显示,您可以使用

echo ( $timestampToDisplay < 60*60*24*365)
    ? Yii::$app->formatter->asRelativeTime($timestampToDisplay )
    : Yii::$app->formatter->asDate($timestampToDisplay );

这将显示

  • 不到一年前的时间戳的相对日期值
  • 超过一年的时间戳的绝对日期值

【讨论】:

    【解决方案2】:
    echo Yii::$app->formatter->asRelativeTime(time());
    

    【讨论】:

      【解决方案3】:

      不幸的是,这里没有人帮助我解决这个问题,但我终于得到了解决这个问题的有用功能。 将时间转换为小时、分钟和...的非常简单的代码。 您只需要使用 crated time 参数调用函数。并返回帖子提交的时间..

      回显动作GetAgoTime($created_time);

      函数 actionGetAgoTime($created_at) {

          $created_at = time() - $created_at; // to get the time since that moment
          $created_at = ($created_at < 1) ? 1 : $created_at;
          $tokens = array(
              31536000 => Yii::t('app', 'year'),
              2592000 => Yii::t('app', 'month'),
              604800 => Yii::t('app', 'week'),
              86400 => Yii::t('app', 'day'),
              3600 => Yii::t('app', 'hour'),
              60 => Yii::t('app', 'minute'),
              1 => Yii::t('app', 'second')
          );
          foreach ($tokens as $unit => $text) {
              if ($created_at < $unit)
                  continue;
              $numberOfUnits = floor($created_at / $unit);
              return $numberOfUnits . ' ' . $text . ' ' . Yii::t('app', 'ago');
          }
      }
      

      【讨论】:

        【解决方案4】:

        您可以轻松地将 timeago jquery 插件用于Yii2:https://github.com/yiidoc/yii2-timeago。请注意,根据您的问题代码,您使用 Jalali 日历库。要使用 timeage 插件,您需要转换您的时间公历。我建议你用时间戳格式保存你的日期,并且你想在模糊形式上显示日期,使用这个插件是这样的:

        // Suppose you pass $comment as an instance of Comment model to your view on controller.
        <?= \yii\timeago\TimeAgo::tag(['timestamp' => date('c', $comment->created_time)]); ?> 
        

        【讨论】:

          猜你喜欢
          • 2019-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-13
          • 2011-02-18
          • 2011-10-10
          • 1970-01-01
          相关资源
          最近更新 更多