84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
= $form->field($model, 'time')->textInput() ?>
问题一:我的time在数据库里是一个时间戳,我现在想显示的是时间,怎么处理一下?
问题二:我在此日期表单中填入 如 2017-05-13 的字符串,在模型中可以写什么方法将它自动转为时间戳?
class XXX extends \yii\db\ActiveRecord { public function rules() { return [ ...other rules... ['time', function($attr, $params) { if ($this->hasErrors()) return false; $datetime = $this->{$attr}; $time = strtotime($datetime); // 验证时间格式是否正确 if ($time === false) { $this->addError($attr, '时间格式错误.'); return false; } // 将转换为时间戳后的时间赋值给time属性 $this->{$attr} = $time; return true; }], ]; } ...others... /** * 从数据库中 find() 数据后将 time 格式化。 */ public function afterFind() { parent::afterFind(); $this->time = date('Y-m-d', $this->time); }