Php laravel 5在数据库中插入日期和时间
我想插入当前日期或日期和时间以及我的表单输入。。。我有这个代码,但我不确定如何包括当前日期Php laravel 5在数据库中插入日期和时间,php,date,datetime,laravel,Php,Date,Datetime,Laravel,我想插入当前日期或日期和时间以及我的表单输入。。。我有这个代码,但我不确定如何包括当前日期 public function create(Patient $patient, Request $request) { // $post = $request->all(); $data = array( 'pName' => $request['fname'], 'pAddress' =>
public function create(Patient $patient, Request $request)
{
// $post = $request->all();
$data = array(
'pName' => $request['fname'],
'pAddress' => $request['address'],
'pBday' => $request['bday'],
'pPhone' => $request['phone'],
'pEcon' => $request['econ'],
'pDreg' => dateTime('created_at')
);
$patient->insert($data);
return redirect('patient');
}
这在PatientController.php中。。请仅使用laravel 5或5.1中的代码laravel将在通过迁移生成新数据库表时自动创建两个
timestamps()
列
Schema::create('tasks', function(Blueprint $table)
{
...
$table->timestamps();
});
编辑:如果要管理时间戳,可以在模型中进行:
class YourModel extends Eloquent
....
public static function boot()
{
public $timestamps = false;
parent::boot();
static::creating(function($model) {
$dt = new DateTime;
$model->created_at = $dt->format('m-d-y H:i:s');
return true;
});
static::updating(function($model) {
$dt = new DateTime;
$model->updated_at = $dt->format('m-d-y H:i:s');
return true;
});
}
参考:
编辑2:
备份参考:调用未定义的函数App\Http\Controllers\timestamp(),这会创建两列,对吗?我只需要在列中创建的_,我想使用我自己的列名。。。我该怎么做?是的,你可以禁用时间戳并手动操作,请参见编辑。我不知道如何操作它,你知道关于碳的知识吗?这容易多了,我就是找不到可靠的消息来源。小心!根据数据库的设置方式,您可能希望以另一种方式定义日期格式。例如:$dt->format('y-m-dh:i:s');