Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

drhardian's avatar

DateTime::__construct(): Failed to parse time string

I need to automatically insert created_at column with manual date for some rules, but it always return

Could not parse '16/01/2023': DateTime::__construct(): Failed to parse time string (16/01/2023) at position 0 (1): Unexpected character

Here is my model,

protected static function boot() {
        parent::boot();

        if(Gate::allows('engineer_progress_manual_date')) {
            static::creating(function ($model) {
                // $model->created_at = Carbon::createFromFormat('d/m/Y H:i:s', request('created_at').' '.date('H:i:s'))->format('Y-m-d H:i:s');
                $model->created_at = DateTime::createFromFormat('d/m/Y H:i:s', request('created_at').' '.date('H:i:s'));
            });
    
            static::updating(function ($model) {
                // $model->created_at = Carbon::createFromFormat('d/m/Y H:i:s', request('created_at').' '.date('H:i:s'))->format('Y-m-d H:i:s');
                $model->created_at = DateTime::createFromFormat('d/m/Y H:i:s', request('created_at').' '.date('H:i:s'));
            });
        }
    }
0 likes
2 replies
frankhosaka's avatar

I'm new in Laravel, but I think that you may not use 'd/m/Y' format to save in database. You must use 'Y-m-d' format.

drhardian's avatar

I have convert it into Y-m-d (please see at commented code) but still got same error

Please or to participate in this conversation.