【问题标题】:how to show unix timestamp in blade view from created_at or updated_at field in database laravel?如何从数据库 laravel 中的 created_at 或 updated_at 字段在刀片视图中显示 unix 时间戳?
【发布时间】:2019-08-19 06:30:38
【问题描述】:

我想在 laravel 5.8 中获取 UNIX 时间戳或将 created_at 或 updated_at 日期从数据库字段(created_at 和 updated_at)转换为 UNIX 时间戳格式,我该怎么做?

这是我在控制器中的代码:

public function viewArchives(){
$data = Archive::select(‘created_at’)->get();
return view(‘view.archives’, compact(‘data’));
}

在 view/archives.blade.php 中:

<?php echo $data;?>

结果是:

{“created_at”:”2019-03-17 19:10:30″}

但我希望结果是这样的:

{“created_at”:”1552849830″}

如何得到这个结果?

【问题讨论】:

    标签: laravel datetime eloquent unix-timestamp


    【解决方案1】:

    使用 Eloquent Model,实际上可以为每列定义 $casts:

    class SomeModel extends Model
    {
    
        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'created_at' => 'datetime'
        ];
    }
    

    如果您想要纪元时间戳,只需删除此演员表(可能存在)。


    当时间戳不是来自任何 Eloquent Model 时,仍然可以使用 Blade 进行渲染:

    @php
    $dt = new DateTime();
    echo $dt->setTimestamp( $timestamp )->format("Y-m-d H:m:s");
    @endphp
    

    或者反过来:

    @php
    echo strtotime( $isodate );
    @endphp
    

    【讨论】:

      【解决方案2】:
      $string = '2019-03-17 19:10:30';
      
      $obj = new stdClass;
      $obj->created_at = $string;
      
      $collection = [$obj];
      
      foreach ($collection as $k => &$v) {
          $collection[$k]->created_at = strtotime($v->created_at);
      }
      
      //var_dump($collection);
      

      或

      $string = '2019-03-17 19:10:30';
      
      $obj = new stdClass;
      $obj->created_at = $string;
      
      $collection = [$obj];
      
      array_walk($collection, function (&$item, $key) {
          $item->created_at = strtotime($item->created_at);
      });
      
      //var_dump($collection);
      

      或者在你的情况下

          public function viewArchives()
          {
              $data = Archive::select('created_at')->get();
              foreach ($data as $k => &$v) {
                  $v->created_at = strtotime($v->created_at);
              }
              return view('view.archives', compact('data'));
          }
      

      或

          public function viewArchives()
          {
              $data = Archive::select('created_at')->get();
              array_walk($data, function (&$item, $key) {
                  $item->created_at = strtotime($item->created_at);
              });
              return view('view.archives', compact('data'));
          }
      

      这是使用array_walk()函数的好地方。

      【讨论】:

      • 感谢@Tpojka,您的解决方案有效
      【解决方案3】:

      添加 strtotime() 视图/archives.blade.php :

      <?php echo strtotime( $data );?>
      

      【讨论】:

      • 使用strtotime是对的,但实际上在他的例子中$data不仅仅是日期字符串,它是一个键值集。
      • @namelivia 你是对的。首先从$data获取created_at值,然后转换strtotime。
      • @namelivia,就像 {{ $data['created_at'] }} 或 {{ $data-&gt;created_at }}
      • @AmitSenjaliya 当我使用它时 >>{{ $data['created_at'] }} 它说:未定义的索引:created_at ...。当我使用这个>>{{ $data-&gt;created_at }}时,它说:这个集合实例上不存在属性[created_at]。
      • @AminArjmand 在您的控制器中将get() 方法更改为toArray()。喜欢$data = Archive::select(‘created_at’)-&gt;toArray(); 并在刀片中使用变量{{ $data['created_at'] }}
      猜你喜欢
      • 2016-08-28
      • 2019-01-22
      • 1970-01-01
      • 2020-07-26
      • 2020-10-10
      • 1970-01-01
      • 2017-09-28
      • 2020-08-19
      • 1970-01-01
      相关资源
      最近更新 更多