fork download
  1. <?php
  2.  
  3. $json = '{
  4. "data": {
  5. "loans": {
  6. "totalCount": 301,
  7. "values": [{
  8. "name": "Anastacia",
  9. "status": "fundRaising",
  10. "plannedExpirationDate": "2017-08-19T22:10:06Z"
  11. },
  12. {
  13. "name": "Mercy",
  14. "status": "fundRaising",
  15. "plannedExpirationDate": "2017-08-19T22:10:05Z"
  16. }
  17. ]
  18. }
  19. }
  20. }';
  21.  
  22.  
  23. // to see the index more clerly we can use this
  24. var_dump(json_decode($json, true));
  25.  
  26.  
  27. // to check for any parsing errors
  28. switch (json_last_error()) {
  29. case JSON_ERROR_NONE:
  30. echo ' - No errors';
  31. break;
  32. case JSON_ERROR_DEPTH:
  33. echo ' - Maximum stack depth exceeded';
  34. break;
  35. case JSON_ERROR_STATE_MISMATCH:
  36. echo ' - Underflow or the modes mismatch';
  37. break;
  38. case JSON_ERROR_CTRL_CHAR:
  39. echo ' - Unexpected control character found';
  40. break;
  41. case JSON_ERROR_SYNTAX:
  42. echo ' - Syntax error, malformed JSON';
  43. break;
  44. case JSON_ERROR_UTF8:
  45. echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  46. break;
  47. default:
  48. echo ' - Unknown error';
  49. break;
  50. }
  51.  
  52. echo "\n";
  53.  
  54. $json_a = json_decode($json, true);
  55.  
  56. //This works:
  57. echo $json_a['data']['loans']['values'][0]['name'];
  58. echo "\n";
  59.  
  60. //this does not:
  61. echo $json_a['data']['loans']['values'][0]['plannedExpirationDate'];
  62. echo "\n";
  63.  
  64. //this does not either. It prints a date in 1970.
  65. $date= $json_a['data']['loans']['values'][1]['plannedExpirationDate'];
  66. echo date('d-m-Y H:i:s', strtotime($date));
  67.  
  68. ?>
Success #stdin #stdout 0s 83904KB
stdin
Standard input is empty
stdout
array(1) {
  ["data"]=>
  array(1) {
    ["loans"]=>
    array(2) {
      ["totalCount"]=>
      int(301)
      ["values"]=>
      array(2) {
        [0]=>
        array(3) {
          ["name"]=>
          string(9) "Anastacia"
          ["status"]=>
          string(11) "fundRaising"
          ["plannedExpirationDate"]=>
          string(20) "2017-08-19T22:10:06Z"
        }
        [1]=>
        array(3) {
          ["name"]=>
          string(5) "Mercy"
          ["status"]=>
          string(11) "fundRaising"
          ["plannedExpirationDate"]=>
          string(20) "2017-08-19T22:10:05Z"
        }
      }
    }
  }
}
 - No errors
Anastacia
2017-08-19T22:10:06Z
19-08-2017 22:10:05