我正在尝试将date_of_birth
列的日期格式从Y-m-d
转换为d-m-Y
。
下面是我的模型:
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Stock extends Model
{
protected $primaryKey = 'tag_no';
public $incrementing = false;
protected $fillable = [
'tag_no',
'stock_type',
'date_of_birth',
];
protected $dates=[
'created_at',
'updated_at',
'deleted_at',
'date_of_birth'
];
public function getDateOfBirthAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d');
}
public function setDateOfBirthAttribute($value)
{
$this->attributes['date_of_birth'] = date('d-m-Y', strtotime($value));
}
}
这是正确的方法吗?
答案 0 :(得分:4)
$in = '2018-08-20';
$out = \DateTime::createFromFormat('Y-m-d', $in)->format('d-m-Y');
print_r($out); // 20-08-2018