fastadmin 后台管理 时间戳字段使用
数据库样式 int 11
后台add.html:
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Lotterytime')}:</label>
<div class="col-xs-12 col-sm-8">
<input >
</div>
</div>
edit.html:
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Lotterytime')}:</label>
<div class="col-xs-12 col-sm-8">
<input >
</div>
</div>
有时会发现这种方法提交会导致数据库中只存了4位的年份值,因为添加编辑提交的是:Y-m-d H:i:s样式,系统只能检测到第一个年份,需要后台controller对应的model转换一下
转换代码:
// 追加属性
protected $append = [
'lotterytime_text'
];
public function getLotterytimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['lotterytime']) ? $data['lotterytime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setLotterytimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}