2 回答

TA貢獻1812條經驗 獲得超5個贊
問題一:
<?=
$form->field($model, 'time')->textInput([ 'value' => date("Y-m-d H:i:s", '時間戳') ])
?>
問題二:
<?php
echo @strtotime('2017-05-13');
?>

TA貢獻1786條經驗 獲得超11個贊
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);
}
- 2 回答
- 0 關注
- 1975 瀏覽
添加回答
舉報