help with data formats (HOW DO I GET PHP to recognize YAHOO's DATES)

Discussion in 'PHP' started by rhythmiccycle, Dec 16, 2007.

  1. #1
    i'm working on some code, that can analyze stock data from yahoo.

    just so you can see what i'm doing this is the code that takes the data from yahoo:

    $url = "http://ichart.finance.yahoo.com/table.csv?s=SPY&a=11&b=16&c=2006&d=11&e=16&f=2007&g=d&ignore=.csv";
    
    $file = fopen($url,"r");
    while(! feof($file))
    {
      $data=fgetcsv($file);
      print_r($data);
      echo"<br>";
    }
    fclose($file); 
    PHP:
    I want the code to know what DAY (mon, tue....) the prices are. yahoo give the date in this format:
    I want the code to recognize this data format and then tell me what days it is. All the web pages i found online just talk about how to format today's date from a time stamp, but thats not what i'm doing.

    i tryed using idate like this: (note: $data[0] is where the date from yahoo is stored)

    
    echo $data[0].
    "<br>".
    idate("Y",$data[0]);
    PHP:
    and i get this out put:

    the code see the Year as1969, but its 2007.

    HOW DO I GET PHP to recognize YAHOO's DATES???????/
    please help
     
    rhythmiccycle, Dec 16, 2007 IP
  2. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could explode the yahoo date string with "-" as delimiter, then you have 3 array fields as result, which contain day, month and year values. You use those values to create timestamp with mktime() and format that timestamp with date() function to the format suitable for you. Take a look at example #1:
    http://www.php.net/manual/en/function.mktime.php

    There is probably better way, but this one works too.

    Just noticed you are using php5.

    Try this:
    idate("Y",strtotime($data[0]));

    Using date() instead of idate() should work fine too.

    http://www.php.net/manual/en/function.strtotime.php
     
    hogan_h, Dec 16, 2007 IP
  3. rhythmiccycle

    rhythmiccycle Guest

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, that helped.

    this is the code is used:
    $time=explode('-',$data[0]);
    	$date=mktime(0,0,0,$time[1],$time[2],$time[0]);
    	echo"<p>".
    	$data[0].
    	"<br>".
    	date("l dS \of F Y",$date). //the whole date
    	"<br>".
    	date("l",$date). //just the day
    	"</p>";
    PHP:
    and this is the output

    i couldn't get strtotime() to work.

    BTW, in i like to use w3schools as my main reference, b/c I like their examples better than php.net. I'd put up a couple of links, but I think I'm still too new to add links.
     
    rhythmiccycle, Dec 16, 2007 IP