Date in JSON Response

Submitted by pratikbutani - 9 years ago

While returning of date field in JSON, I found some unexpected output which i have posted on stackoverflow. You can see here for more information: http://stackoverflow.com/q/31184170/1318946

//PRINTING ARRAY Directly in KEY `data`
$accountData = Account::select('name', 'status', 'id', 'user_id', 'utp')->where('user_id', Auth::id())->first();
$return = array(
                'result' => 'success',
                'msg' => 'Login Successfully.',
                'data' => $accountData
               );
return Response::json($return);

/** Output of about code is like (Specially See 'utp' field.): */
/*
{
  "result": "success",
  "msg": "Login Successfully.",
  "data": {
    "name": "Demo",
    "status": 0,
    "id": 143,
    "user_id": 207,
    "utp": "2015-07-01 18:38:01"
  }
}
*/
/******************/

// PRINTING Each Value Separately
$return = array(
    'result' => 'success',
    'msg' => 'Login Successfully.',
    'data' => $accountData['user_id'],
    'account_id' => $accountData['id'],
    'utp' => $accountData['utp'],
    'usertype' => 'account',
    'status' => $accountData['status']
);
return Response::json($return);

/** OUTPUT: SEE `utp` value printing different form **/
/*
{
  "result": "success",
  "msg": "Login Successfully.",
  "data": 207,
  "account_id": 143,
  "utp": {
    "date": "2015-07-01 18:38:01",
    "timezone_type": 3,
    "timezone": "Asia\\/Kolkata"
  },
  "usertype": "account",
  "status": 0
}
*/

/******************/

// NOW Finally got the ANSWER to print only date in separate value.
/** 'utp' => $accountData['utp']->format("Y-m-d H:i:s") **/
$return = array(
    'result' => 'success',
    'msg' => 'Login Successfully.',
    'data' => $accountData['user_id'],
    'account_id' => $accountData['id'],
    'address' => $accountData['address'],
    'utp' => $accountData['utp']->format("Y-m-d H:i:s"),
    'usertype' => 'account',
    'status' => $accountData['status']
);