2017-09-02 140 views
0

如何显示在表单上我的当前系统(PC)的时间显示当前系统日期和时间

现在,我目前正在使用一个DateTimePicker,我想尝试当我要创造一个东西形成“time_start”会自动获得我的电脑上正在使用的时间。

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

PH P在服务器上运行而不是您的电脑。它将使用服务器时间,除非您以不同的方式说明。你将不得不把时间从PC发送到PHP脚本。然后它可以计算出在'日期选择器'中使用的时间 –

回答

1

您可以分配值$模型 - > TIME_START直接在控制器打电话之前渲染..

,所以你的问题可以在controllerAction使用PHP载体作用的分配,需要安装价值管理在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, 
     ]); 
    } 
} 

,或者如果你需要一个特定的时区

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, 
     ]); 
    } 
} 
+0

感谢它工作正常,它显示正确的日期,但不是时间?我认为它是因为时区,我该如何更改我的时区 – noobkoder

+0

@noobkoder答案更新 – scaisEdge

+0

我现在知道我不需要更改时区,因为它会自动从服务器获取当前系统日期和时间,我的问题是它没有显示正确的时间,当我创建窗体,但如果窗体已经创建它显示正确的时间大声笑我不明白为什么它如此混乱。无论如何感谢帮助它真的帮助我很多 – noobkoder