title: PHP get CSV from URL, load into array, format
body: <p>I am trying to write a script to download historic data from yahoo finance as a csv.  My script successfully loads the data into an array but I've run into two problems.  First is that despite creating a date range for it to pull from yahoo, I keep getting the entire historical data for that stock and I'm not sure why.I only want the last 6months calculated back from current day.  Second, I was able to use str_getcsv to load the data into an array, however I have been unable to create a loop that will work to set it up in a table with the first row being the headers of the table columns and the rest organized by date in rows.   </p>
  
  <p>Here is the code:</p>
  
  <pre><code>&lt;?php
  
  $stock = "AAPL";
  $date = strtotime(date('Y-m-d') . ' -1 month');
  $date2 = strtotime(date('Y-m-d') . ' -6 months');
  
  $a = (date('m', $date));
  $b = (date('d', $date));
  $c =(date('Y', $date));
  $d = (date('m', $date2));
  $e = (date('d', $date2));
  $f =(date('Y', $date2));
  
   $s = str_getcsv(file_get_contents("http://ichart.yahoo.com/table.csv?s=$stock&amp;a=$d&amp;b=e&amp;c=$f&amp;d=$a&amp;e=$b&amp;f=$c&amp;g=d"));
  
  
  Stock:echo $stock;
  
  echo '&lt;pre&gt;';
  print_r($s);
  echo '&lt;/pre&gt;';
  ?&gt;
  </code></pre>
  
  <p>and here is the output:</p>
  
  <pre><code>AAPL
  
  Array
  (
      [0] =&gt; Date
      [1] =&gt; Open
      [2] =&gt; High
      [3] =&gt; Low
      [4] =&gt; Close
      [5] =&gt; Volume
      [6] =&gt; Adj Close
  2014-10-10
      [7] =&gt; 100.69
      [8] =&gt; 102.03
      [9] =&gt; 100.30
      [10] =&gt; 100.73
      [11] =&gt; 66270200
      [12] =&gt; 100.73
  2014-10-09
      [13] =&gt; 101.54
      [14] =&gt; 102.38
      [15] =&gt; 100.61
      [16] =&gt; 101.02
      [17] =&gt; 77312200
      [18] =&gt; 101.02
  2014-10-08
      [19] =&gt; 98.76
      [20] =&gt; 101.11
      [21] =&gt; 98.31
      [22] =&gt; 100.80
      [23] =&gt; 57364800
      [24] =&gt; 100.80
  2014-10-07
      [25] =&gt; 99.43
      [26] =&gt; 100.12
      [27] =&gt; 98.73
      [28] =&gt; 98.75
      [29] =&gt; 42068200
      [30] =&gt; 98.75
  </code></pre>
  
  <p>etc...</p>
  
  <p>Any help would be greatly appreciated!</p>