2

I have a posts Model & Controller and when I try to get all posts, it returns the created_at and updated_at colums as this: "created_at": "2020-05-31T22:04:38.000000Z",

As you can see, there is a weird "T" between the date and time + there's ".000000Z" at the end.

I tried using Carbon::setToStringFormat(); but no luck.

This is my PostsController@index

    {
        Carbon::setToStringFormat(DateTime::ISO8601);
        $posts = Post::with('author:id,name,lastName')->get();
        return response()->json(['posts' => $posts->toArray()],200);
    }

And this problem persists on every single model/migration I have made. Including the users table.

"users": [
    {
      "id": 1,
      "email": "admin@test.com",
      "status": "admin",
      "name": "Administrators",
      "lastName": "Uzvārds",
      "department": "client",
      "phone": "null",
      "created_at": "2020-05-31T22:03:36.000000Z",
      "updated_at": "2020-05-31T22:03:36.000000Z"
    },
    {
      "id": 2,
      "email": "user@test.com",
      "status": "employee",
      "name": "Darbinieks",
      "lastName": "Uzvārds",
      "department": "client",
      "phone": "null",
      "created_at": "2020-05-31T22:03:36.000000Z",
      "updated_at": "2020-05-31T22:03:36.000000Z"
    }
  ]

As you can see, in PHPMyAdmin everything's completely fine: enter image description here

HenrijsS
  • 656
  • 4
  • 25

1 Answers1

4

That’s not “weird”. It’s pretty standard ISO8601 format with a microsecond precision and “Z” (ie. UTC) time zone designation. The ISO8601 format allows a “T” between date and time components, as well as several other variations.

The database display happens to use a different variation. This is OK and expected because the database type is a DATETIME (or similar), so the displayed text is only a given representation. Both representations represent the same date and time^.

This is a good format to transfer information (and is common in JSON serialization), less so to display to a user.

^ Assuming the data in the database is stored from a UTC source, which appears to be the case here. If it’s a local time source there are other complications as the “Z” offset in the JSON would be incorrect/misleading without value adjustments.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    JavaScript’s [Date.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) will accept that format. Many JS framework will contain date/time formatting helpers (see specific library support). I’ve had luck with the [moment.js library](https://momentjs.com/), and find it easy to use for conversions to/from human-readable values. – user2864740 May 31 '20 at 22:34
  • Alternatively, one could format the date to a _string_ in PHP, and add that to the objects being serialized to JSON. See [PHP’s strftime](https://www.php.net/manual/en/function.strftime.php) as one method. – user2864740 May 31 '20 at 22:38
  • 1
    Thanks @user2864740 this is what I needed! I believe that should be edited to answer my question. – HenrijsS May 31 '20 at 22:46