A
A
AnotherUniverse2020-02-18 22:52:48
PostgreSQL
AnotherUniverse, 2020-02-18 22:52:48

What value should I set the $dateFormat Eloquent property to work correctly with a date and time string received from PostgreSQL?

Good day.

The project uses Laravel and PostgreSQL12.
I get a date and time string from the database in the format "2020-02-18 19:00:00.761514+02".
In the model, Eloquent defined the format in the $dateFormat property as follows:
protected $dateFormat = 'Ymd H:i:s.uO';

Everything was fine until I saved the following value to the database:
"2020-02-18 19:00:00.000000+0200".
The entry was saved, but now Postgres returns the string "2020-02-18 19:00:00+02" to me.
As I understand it, if the fractal part is zero, then Postgres ignores it when outputting.
As a result, such a line crashes the application, because does not match the format.

The first thing that comes to mind is to change the types of the date and time columns in the database to timestamptz(0), and the format in the models to "Ymd H:i:sO" and not bother with the fractal part at all. But I don't really want to do it for various reasons.
The second is to process the dates manually with additional checks. The option, in my opinion, is even worse.

Is it possible to somehow change the format string so that the presence or absence of a fractal part does not break the application?
I smoked the manual, but did not find an obvious way to do this. Maybe I smoked badly))

Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question