Skip to content

Date fields in SQL database are returned as invalid date string in Laravel #28491

@robinnydahl

Description

@robinnydahl
  • Laravel Version: 5.8.16
  • PHP Version: 7.3.5
  • Database Driver & Version: MSSQL / Microsoft SQL Server 2016 - 13.0.5292.0

Description:

When we fetch dates either from direct DB statement or Eloquent we get the date field in the SQL database returned as an invalid formatted date string like Sep 12 2018 12:00:00:AM.

First of all, the last : before AM results in Carbon giving an exception as it's an invalid date format. With that removed Carbon can recognize the date again.

However we can't seem to figure out why Laravel formats this date in the first place. We have no models for the given table (no dates are mutated) and the date is stored as YYYY-MM-DD in the database.

An example DB statement is DB::connection('database')->table('table')->get()

We've tested this with MySQL, the problem is not present there. Once we switch back to MSSQL the issue returned.

Steps To Reproduce:

  1. Fetch a record from the database where a field with type date is present.
  2. The returned record is formatted invalid with Sep 12 2018 12:00:00:AM, should be 2018-09-12.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions