dongnaoxia0927 2017-09-02 18:11
浏览 89

显示当前系统日期和时间

How do I show my current system (PC) time on a form

Right now I'm currently using a datetimepicker, I wanna try when I'm going to create something in a form the "time_start" will automatically get the time that is being used on my PC.

<?= $form->field($model, 'time_start')->widget(
    DateTimePicker::className(), [
        'options'       => [ 'placeholder' => 'Render Time' ],
        'pluginOptions' => [ 'autoclose' => true, ]
    ]
); ?>
  • 写回答

1条回答 默认 最新

  • dssnh86244 2017-09-02 18:22
    关注

    You can assign the value to $model->time_start directly in your controller before call the render ..

    so you problem can be managed in controllerAction using php functin for assign the value you need eg in actionCreate

    public function actionCreate()
    {
        $model = new MyModel();
    
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            //  assign the the date and time of the server that the code runs on.
            $model->time_start =  date("d-m-Y H:i:s");
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    

    or if you need an specific timezone

    public function actionCreate()
    {
        $model = new MyModel();
    
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            //  assign the the date and time of the server that the code runs on.
            //  
            $my_date = new DateTime("now", new DateTimeZone('America/New_York') );
    
            $model->time_start =   $my_date;
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan