Php 使用Laravel和Carbon将用户提供的1970年之前的日期插入MySql
我正在使用以下代码:Php 使用Laravel和Carbon将用户提供的1970年之前的日期插入MySql,php,mysql,laravel,php-carbon,Php,Mysql,Laravel,Php Carbon,我正在使用以下代码: $birthday = request('birthday_day') . "-" . request('birthday_month') . "-" . request('birthday_year'); $birthday = Carbon::createFromFormat('d-m-Y', $birthday); $min_year = date('Y') - 10; if ($birthday->gte(Carbon::create
$birthday = request('birthday_day') . "-" . request('birthday_month') . "-" . request('birthday_year');
$birthday = Carbon::createFromFormat('d-m-Y', $birthday);
$min_year = date('Y') - 10;
if ($birthday->gte(Carbon::create($min_year, 1, 1))) {
return back()->withErrors(['message' => 'You need to be at least 10 years old to sign up.'])->withInput();
}
$user = User::create([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
'password' => bcrypt(request('password')),
'birthday' => $birthday, //THE PROBLEM
'gender' => request('gender'),
'verified' => 0,
'verification_code' => bcrypt(str_random(30) . request('email'))
]);
运行时,我收到以下错误消息:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1943-01-17 08:45:49' for column 'birthday' at row 1 (SQL: insert into `users` (`first_name`, `last_name`, `email`, `password`, `birthday`, `gender`, `verified`, `verification_code`, `updated_at`, `created_at`) values (Name, name2, example@example.com, $2y$10$qOMKZBGF4rsyEtkH2Cd5CuPDshwN7ULEKWfakrvDTrIo5KQTWTO.i, 1943-01-17 08:45:49, male, 0, $2y$10$oU88Yddwug6dgapv./REJ.NTWlCHKMXOUHVyOGd2UJXCqvHpIIW3y, 2017-03-19 08:45:49, 2017-03-19 08:45:49))
这只发生在年份小于1970年的日期。由于unix和mysql的时间戳,我了解到这是如何发生的,但似乎无法修复这一问题
迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password');
$table->timestamp('birthday');
$table->string('gender');
$table->integer('verified')->default(0);
$table->string('verification_code')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
我认为您需要在生日栏上使用datetime
类型:
Schema::create('users', function (Blueprint $table) {
...
$table->datetime('birthday');
...
});
时间戳只能在1970年后为正,因为它的定义我认为您需要在生日栏上使用datetime
类型:
Schema::create('users', function (Blueprint $table) {
...
$table->datetime('birthday');
...
});
时间戳只能在1970年后为正,因为它的定义您能否为此表提供迁移文件?@El_Matella编辑了答案。您能否为此表提供迁移文件?@El_Matella编辑了答案。