doubu7425 2016-01-20 07:58
浏览 140

使用Yii2中的模型值设置日期函数

Hi i have function where i need to take values from dropdownlist(in 3 dropdownlists i have year, month, day like in facebook registration). I want to set date from this 3 values.

public function setDate()
{
   $month=$this->month+1;
   $date = date_create();
   date_date_set($date, $this->year,$month, $this->day);
   return date_format($date, 'Y-m-d');

}

then in my controller i want to save my date but it doesnt work.

 if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $model->setDate();
        $model->save();

Which method of date can i use to this operation where i can give my values in parameters?

i try this method too

public function setDate()
{
   $month=$this->month+1;
   $date = new DateTime();
   $date ->setDate($this->year,$month, $this->day);
   return $date->format('Y-m-d');

}

but i have

Argument 1 passed to Faker\Provider\Base::__construct() must be an instance of Faker\Generator, none given, called in E:\htdocs\mesport\frontend\modules\settings\models\Profile.php on line 119 and defined
  • 写回答

1条回答 默认 最新

  • dongyuan1984 2016-01-20 09:14
    关注

    In your custom method you actually don't change the model attribute value. Assuming this method is located inside model, replace return line by:

    $this->yourDateAttribute = ...;
    

    Also make sure this attribute is safe in current scenario.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器