Skip to content

Instantly share code, notes, and snippets.

@samsch
Last active October 11, 2019 15:48
Show Gist options
  • Save samsch/db909e5fa57bb56fe2817a03cc265936 to your computer and use it in GitHub Desktop.
Save samsch/db909e5fa57bb56fe2817a03cc265936 to your computer and use it in GitHub Desktop.
Working with Postgres JSON datetimes in Laravel

Working with Postgres JSON datetimes

By default Laravel uses Y-m-d H:i:s for datetime parsing and formatting against the database. It gets this value as the connection default.

Postgres uses Y-m-d H:i:s as the datetime format default for all fields. Except that it does not follow this for datetimes that are serialized as JSON, where it follows ISO 8601 and formats as Y-m-d\TH:i:s.

Laravel is fortunately able to use a manually set datetime format, so after you hydrate a model with datetime from Postgres JSON with datetimes, you should call $modelInstance->setDateFormat('Y-m-d\TH:i:s'); so that datetimes are parsed correctly. Fortunately, this does not break writing to the db, which seems quite happy to accept ISO 8601 datetime strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment