laravel时间格式转时间戳的例⼦
数据渲染到模板经常⽤到⽇期格式。⽽数据库⼀般保存时间戳。每次更新或查询都要做转换。
使⽤
Eloquent
⾃动转换
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Goods extends Model
{
public function setStartTimeAttribute($value)
{
$this->attributes['start_time'] = is_int($value) ? $value : strtotime($value);
}
public function getStartTimeAttribute()
{
return date('Y-m-d H:i:s', $this->attributes['start_time']);
}
}
⽅法名称应与被转换字段名称相同
以上这篇
laravel
时间格式转时间戳的例⼦就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀
持。

浙公网安备 33010602011771号