Cast datetime from JSON string? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29) +--- Thread: Cast datetime from JSON string? (/showthread.php?tid=88938) Pages:
1
2
|
Cast datetime from JSON string? - ozornick - 12-05-2023 I have discovered a new behavior for Datetime Cast https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/Entity/Cast/DatetimeCast.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 RE: Cast datetime from JSON string? - kenjis - 12-05-2023 See https://github.com/codeigniter4/CodeIgniter4/pull/8230 RE: Cast datetime from JSON string? - ozornick - 12-05-2023 No, in PR DatetimeCast apply only string value. But Json have array: created_at => [ date, timezone, timezone_type] Not simple "date* string RE: Cast datetime from JSON string? - kenjis - 12-05-2023 You need to write a custom cast handler for it. By the way, why is your date in JSON strange format like that? RE: Cast datetime from JSON string? - ozornick - 12-06-2023 Hmm, its default DatetimeCast converter. PHP Code: // Project entity RE: Cast datetime from JSON string? - kenjis - 12-06-2023 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.datetimeinterface.php#datetimeinterface.constants.types RE: Cast datetime from JSON string? - ozornick - 12-06-2023 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: /** RE: Cast datetime from JSON string? - kenjis - 12-06-2023 The current Entity is somewhat broken. I don't know what toArray() is. RE: Cast datetime from JSON string? - ozornick - 12-06-2023 Return all casted properties, and not just strings https://github.com/codeigniter4/CodeIgniter4/blob/7a382063e4863006aad058fff3582bf4db9094e8/system/Entity/Entity.php#L166 RE: Cast datetime from JSON string? - kenjis - 12-06-2023 In my opinion, casts in Entity is broken. and toRawArray() does not return raw value (string) if it is Time object. |