dbol Posted May 30, 2010 Share Posted May 30, 2010 Here is my code: <?php $xml_file = "http://www.meltwaternews.com/magenta/xml/html/37/10/rss/147839.rss.XML"; $xml_title_key = "*RDF:RDF*ITEM*TITLE"; $xml_url_key = "*RDF:RDF*ITEM*LINK"; $xml_ingress_key = "*RDF:RDF*ITEM*DC:DESCRIPTION"; $xml_date_key = "*RDF:RDF*ITEM*DC:DATE"; $xml_source_key = "*RDF:RDF*ITEM*DC:SOURCE"; $document_array = array(); $counter = 0; class xml_document{ var $title, $url, $ingress, $date, $source, $dateMonth, $dateDay, $dateYear; } function startTag($parser, $data){ global $current_tag; $current_tag .= "*$data"; } function endTag($parser, $data){ global $current_tag; $tag_key = strrpos($current_tag, '*'); $current_tag = substr($current_tag, 0, $tag_key); } function contents($parser, $data){ global $current_tag, $xml_title_key, $xml_url_key, $xml_ingress_key, $xml_date_key, $xml_source_key, $counter, $document_array; switch($current_tag){ case $xml_title_key: $document_array[$counter] = new xml_document(); $document_array[$counter]->title = $data; break; case $xml_url_key: $document_array[$counter]->url = $data; break; case $xml_ingress_key: $document_array[$counter]->ingress = $data; break; case $xml_date_key: $document_array[$counter]->date = $data; break; case $xml_source_key: $document_array[$counter]->source = $data; $counter++; break; } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startTag", "endTag"); xml_set_character_data_handler($xml_parser, "contents"); $fp = fopen($xml_file, "r") or die("Could not open file"); function remotefsize($furl) { $sch = parse_url($furl, PHP_URL_SCHEME); if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) { return false; } if (($sch == "http") || ($sch == "https")) { $headers = get_headers($furl, 1); if ((!array_key_exists("Content-Length", $headers))) { return false; } return $headers["Content-Length"]; } } $data = fread($fp, remotefsize("http://www.meltwaternews.com/magenta/xml/html/37/10/rss/147839.rss.XML")) or die("Could not read file"); if(!(xml_parse($xml_parser, $data, feof($fp)))){ die("Error on line " . xml_get_current_line_number($xml_parser)); } xml_parser_free($xml_parser); fclose($fp); ?> <html> <head> <title>Project: Parse XML 4</title> </head> <body bgcolor="#FFFFFF"> <?php for($x=0;$x<count($document_array);$x++){ echo "<b>\t" . $document_array[$x]->title . "</b>\n<br/>"; echo "\t" . $document_array[$x]->date . " | " . $document_array[$x]->source . "\n<br/>"; echo "\t" . $document_array[$x]->ingress . "\n<br/>"; echo "\t<a href='" . $document_array[$x]->url . "'>" . $document_array[$x]->url . "</a>\n<br/><br/>"; } ?> </body> </html> I want to substring the date so that I can extract the year, date, and month as separate variables, and then reformat them to MM/DD/YYYY. The date currently looks like: 2010-05-29T23:52:40+00:00 I want it to look like 05/29/2010 Any ideas? Link to comment https://forums.phpfreaks.com/topic/203379-how-do-i-substring-a-variable-in-an-array/ Share on other sites More sharing options...
Mchl Posted May 30, 2010 Share Posted May 30, 2010 date('m/j/Y',strtotime('2010-05-29T23:52:40+00:00')); Link to comment https://forums.phpfreaks.com/topic/203379-how-do-i-substring-a-variable-in-an-array/#findComment-1065449 Share on other sites More sharing options...
dbol Posted May 30, 2010 Author Share Posted May 30, 2010 Where in the code is that going to go? The dates are all called with a variable, so I can't just pass that specific date string I pasted earlier in to your converter. That was just an example. I have to pass the variable, $date, or something like it. Link to comment https://forums.phpfreaks.com/topic/203379-how-do-i-substring-a-variable-in-an-array/#findComment-1065451 Share on other sites More sharing options...
Mchl Posted May 30, 2010 Share Posted May 30, 2010 What's stopping you? Just put your variable instead of date string. $date = '2010-05-29T23:52:40+00:00'; $dateFormatted = date('m/j/Y',strtotime($date)); echo $dateFormatted; Link to comment https://forums.phpfreaks.com/topic/203379-how-do-i-substring-a-variable-in-an-array/#findComment-1065455 Share on other sites More sharing options...
dbol Posted May 30, 2010 Author Share Posted May 30, 2010 I got it. Works great. Thanks so much for your help. Link to comment https://forums.phpfreaks.com/topic/203379-how-do-i-substring-a-variable-in-an-array/#findComment-1065456 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.