Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/03/2020, 15:45
dar2
 
Fecha de Ingreso: octubre-2011
Ubicación: Barcelona
Mensajes: 5
Antigüedad: 12 años, 6 meses
Puntos: 0
Información Obtener datos en JSON por años

Buenas, mi pregunta es la siguiente:
Como podría obtener los valores dentro un rango de años determinado? Por ejemplo: obtener todos los valores comprendidos entre la fecha 2020-03-13 y 2016-01-01.

Código:
  public function getDataJson()
    {
           $contents = '{
                        "Meta Data": {
                                "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
                                "2. Symbol": "MSFT",
                                "3. Last Refreshed": "2020-03-13",
                                "4. Time Zone": "US/Eastern"
                            },
                            "Monthly Time Series": {
                                "2020-03-13": {
                                    "1. open": "165.3100",
                                    "2. high": "175.0000",
                                    "3. low": "138.5800",
                                    "4. close": "158.8300",
                                    "5. volume": "691392295"
                                },
                                "2020-02-28": {
                                    "1. open": "170.4300",
                                    "2. high": "190.7000",
                                    "3. low": "152.0000",
                                    "4. close": "162.0100",
                                    "5. volume": "887894931"
                               },
                                "2020-01-31": {
                                    "1. open": "158.7800",
                                    "2. high": "174.0500",
                                    "3. low": "156.5100",
                                    "4. close": "170.2300",
                                    "5. volume": "555989763"
                                },
                                "2019-12-31": {
                                    "1. open": "151.8100",
                                    "2. high": "159.5500",
                                    "3. low": "146.6500",
                                    "4. close": "157.7000",
                                    "5. volume": "452664147"
                                }
                              }
                       }';

        $contents = utf8_encode($contents);

        return $contents;
}

public function readDataJSONAction()
    {
        $cotizacion  = new Cotizacion();
        $dataJson = $cotizacion->getDataJson();
        $data = json_decode($dataJson, true);
        //$cot = $data['Monthly Time Series']['2020-01-31']['1. open'];//Muestra el valor de apertura de la fecha indicada: 165.31

        return $this->render(':cotizacion:index.html.twig',
            [
                'data' => $data
            ]);
    }

index.html.twig

{{ data['Monthly Time Series']['2020-03-13']['1. open'] }}

Gracias por vuestra respuesta, llevo todo el día intentando darle la mejor solución pero no acabo de dar con ella.