-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Entity::jsonSerialize() returns strange dates #8303
Comments
Seems like the default behavior for the If we want to change it, the question is, do we really want to lose the time zone information? |
Yes, it defaut action for JSON dates. Timezone may be set |
Yes. Because this issue is about the default behavior. Whether timezone is required or not depends on requirement. |
I don't have a strong opinion about this, but I wouldn't call it a bug. If we want to change this behavior that should be considered as a breaking change since the default value for JSON will change. Developers who have built an API may rely on this because this is the default behavior in PHP. |
While it may be true that changing this will be a breaking change, it seems unreasonable to say that this is a specification. |
Why don't we know? This shows what type of zone is displayed. To restore an object, it is not important, you only need the date and timezone. Why not add array support with these keys to |
Because it is not documented. I don't know the exact meaning.
What do you mean? |
There seem to be maybe three values for |
|
We studied this when there was a bug with Datetime serialization. Don't you remember? |
<?php
namespace App\Entities\Cast;
use CodeIgniter\Entity\Cast\BaseCast;
use CodeIgniter\Entity\Cast\DatetimeCast;
use CodeIgniter\I18n\Time;
use DateTimeZone;
/**
* Adds date conversion from a JSON array as:
* [
* 'date' => '2000-11-25 10:20:35.000000'
* 'timezone_type' => 3
* 'timezone' => 'Europe/Moscow'
* ]
*/
class ExtendedDatetimeCast extends BaseCast
{
public static function get($value, array $params = [])
{
return DatetimeCast::get($value, $params);
}
public static function set($value, array $params = [])
{
if (is_array($value) && array_key_exists('date', $value) && array_key_exists('timezone', $value)) {
return DatetimeCast::get(new Time($value['date'], new DateTimeZone($value['timezone'])));
}
return DatetimeCast::set($value, $params);
}
} |
I don't think it is a bug because if you want the expected output you can just remove the
and it should work as you wanted. |
The current behavior is the behavior of PHP's echo json_encode(new DateTime('now'));
// {"date":"2024-02-08 22:57:15.503235","timezone_type":3,"timezone":"UTC"} |
PHP Version
8.1
CodeIgniter4 Version
4.4.3 and develop
CodeIgniter4 Installation Method
Git
Which operating systems have you tested for this bug?
macOS
Which server did you use?
cli-server (PHP built-in webserver)
Database
MySQL 8.0.34
What happened?
Entity with dates returns JSON with strange format of dates.
Steps to Reproduce
Expected Output
Anything else?
See also #8302
The text was updated successfully, but these errors were encountered: