将带有时间戳值的int列转换为日期格式(Y-m-d)

时间:2019-03-29 19:47:22

标签: php sql-server laravel-5

我有一个称为(date)的sql-server列,其类型为int类型,此列的值是时间戳记,如(1538254800),我需要从laravel sql查询中选择此列并将其显示为日期格式(Ymd H:m :i pm)like(2018-09-29 09:00:00 pm)

1 个答案:

答案 0 :(得分:0)

演员

https://laravel.com/docs/5.8/eloquent-mutators#attribute-casting

示例:

class MyModel extends Model {
  // ...
  protected $casts = [
    'int_col_name' => 'datetime:u',
  ];
  // ...
}

日期修改器

https://laravel.com/docs/5.8/eloquent-mutators#date-mutators

示例:

class MyModel extends Model {
  // ...
  protected $dates = [
    'int_col_name',
  ];
  // ...
}