Cast datetime from JSON string? |
I have discovered a new behavior for Datetime Cast https://github.com/codeigniter4/CodeIgni...meCast.php It can only recover from a string or a number. But for the API, the JSON result has an array
PHP Code: array(3) { Do you have any ideas how to do this better? Just taking the date may be wrong, the time zone will be lost
No, in PR DatetimeCast apply only string value.
But Json have array: created_at => [ date, timezone, timezone_type] Not simple "date* string
You need to write a custom cast handler for it.
By the way, why is your date in JSON strange format like that?
Hmm, its default DatetimeCast converter.
PHP Code: // Project entity
Isn't it a bug?
If you need datetime with timezone, it is better to use string with timezone like "2005-08-15T15:52:01+00:00". https://www.php.net/manual/en/class.date...ants.types
This is the behavior of the built-in DatetimeCast. It's not particularly critical for me, I added a new ExtDatetimeCast
Perhaps it is worth adding a special exception for dates in the Entity? toArray() executes castAs() and therefore the JSON gets not a string, but an object PHP Code: /**
The current Entity is somewhat broken.
I don't know what toArray() is.
Return all casted properties, and not just strings
https://github.com/codeigniter4/CodeIgni...y.php#L166
In my opinion, casts in Entity is broken.
and toRawArray() does not return raw value (string) if it is Time object. |
Welcome Guest, Not a member yet? Register Sign In |