在 Mysql 数据库中插入日期值
Insert date value in Mysql DB
我正在尝试像这样在 MySQL table 中插入一个日期值(使用 Laravel 5.1):
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$data = $request->all();
$data['birthdate'] = date("Y-d-m", strtotime($data['birthdate']));
...
$user->fill($data)->save();
如果我插入一个 < 12 日的日期,它工作正常,而如果日 > 12,它将被插入 1970-01-01 !
我在用户模型中使用了 Eloquent 修改器,如下所示:
public function getBirthdateAttribute() {
return date('d/m/Y', strtotime($this->attributes['birthdate']));
}
public function setBirthdateAttribute($value) {
$this->attributes['birthdate'] = Carbon::createFromFormat('Y-m-d', $value);
}
请问我的代码怎么了!?
改变这个:
date("Y-d-m", strtotime($data['birthdate']));
至:
date("Y-m-d", strtotime($data['birthdate']));
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-literals.html
你应该像这样的格式 dd-mm-yyyy
,如果像这样 dd/mm/yyyy
$date = '22/05/2012';
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
这是您需要阅读和理解的strtotime() manual部分
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
所以因为您使用 /
作为分隔符 strtotime() 假设美国日期格式并且混淆了日期和月份部分
我正在尝试像这样在 MySQL table 中插入一个日期值(使用 Laravel 5.1):
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$data = $request->all();
$data['birthdate'] = date("Y-d-m", strtotime($data['birthdate']));
...
$user->fill($data)->save();
如果我插入一个 < 12 日的日期,它工作正常,而如果日 > 12,它将被插入 1970-01-01 !
我在用户模型中使用了 Eloquent 修改器,如下所示:
public function getBirthdateAttribute() {
return date('d/m/Y', strtotime($this->attributes['birthdate']));
}
public function setBirthdateAttribute($value) {
$this->attributes['birthdate'] = Carbon::createFromFormat('Y-m-d', $value);
}
请问我的代码怎么了!?
改变这个:
date("Y-d-m", strtotime($data['birthdate']));
至:
date("Y-m-d", strtotime($data['birthdate']));
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-literals.html
你应该像这样的格式 dd-mm-yyyy
,如果像这样 dd/mm/yyyy
$date = '22/05/2012';
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
这是您需要阅读和理解的strtotime() manual部分
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
所以因为您使用 /
作为分隔符 strtotime() 假设美国日期格式并且混淆了日期和月份部分