any casts on the model?
Why do my ‘date’-type columns have a colon after the seconds?
I have a model with two date/time columns: added stored as a datetimeoffset, and published as a date in the database (SQL Server). If both refer to today, they’d be stored stored as 2022-02-09 00:00:00.0000000 +00:00 and 2022-02-09, respectively.
When I retrieve them, though, they are altered – and the latter is broken. If I retrieve an instance of the model, or even if I just select the columns directly with DB::table(…), and immediately dd() the result, this is what I get:
datetime column: "2022-02-09 00:00:00.0 UTC (+00:00)" // modified, but fine
date column: "Feb 9 2022 12:00:00:AM" // WTF?
The date-type column is now represented as text, American-style, and uses 12-hour time – and it has a really bizarre, rogue colon between the seconds and AM. If I access the property directly (lazy-loading it as a Carbon date), I get an InvalidFormatException since Carbon cannot parse the string because of this colon.
If I retrieve the column using a plain PDO connection from somewhere unconnected to Laravel, I get the expected 10-character string back, so it must be Laravel/Eloquent doing something odd here.
In this particular case, I don’t think changing the colum to a datetimeoffset should be an issue, but why (and where) is the date transformed to American text-style, and where on earth does that extra comma come from? (I mean, it presumably comes from something somewhere that’s supposed to do something with milliseconds, but why aren’t the milliseconds actually added then, or indeed the space after?)
This just returned today out of the blue, but this time I had an inkling what might have caused it: I recently updated to PHP 8.2. Combined with this GitHub comment (which hadn’t been posted yet when I asked about this last year), I tracked down the issue.
The PHP extensions sqlsrv and pdo_sqlsrv are not installed by default (at least not on macOS when managing things through Homebrew), which means that Laravel uses the ODBC driver by default. And the ODBC driver, for whatever reason, Eloquent cannot handle date columns when using the ODBC driver.
When I updated to PHP 8.2, I had a fresh PHP install without the correct extensions, so the issue came back. Installing the newest version of them (5.11.0, released in January with support for PHP 8.2) fixed it. (Note that you may have to add some extra flags for the install to work on macOS; see this GitHub comment.)
Please or to participate in this conversation.