开发者

Change XML date from yyyymmdd to Month Day, Year

开发者 https://www.devze.com 2023-03-05 00:49 出处:网络
I\'m using PHP to output data from an XML file, specifically the date, which outputs as \"20101110.\"

I'm using PHP to output data from an XML file, specifically the date, which outputs as "20101110."

Can I use PHP to change this to November 10, 2011? If so, how?

Here's my page and code:

    $file = 'http://www.gostanford.com/data/xml/events/m-baskbl/2010/index.xml';
    $xml = simplexml_load_file($file);

    foreach($xml as $event_date){
        if(!empty($event_da开发者_C百科te->event['vn']) && !empty($event_date->event['hn']) && !empty($event_date->event['vs']) && !empty($event_date->event['hs']))
        { 
            echo '<li>';
                echo '<h3>', $event_date->event['vn'], ' vs ', $event_date->event['hn'], '</h3>';
                echo '<p><strong>', $event_date->event['vs'], ' - ', $event_date->event['hs'], '</strong></p>';
                echo '<p>', $event_date['date'], '</p>';
                echo '<p>', $event_date->event['local_time'], '</p>';
            echo '</li>';   
        }
    }

Thanks for your help!


You can use the date() function, in combination with the strtotime() function:

echo date('F j, Y', strtotime($event_date['date']));


Sure, here's an example function:

function toRealDate($str) {
    $year = substr($str, 0, 4);
    $month = substr($str, 4, 2);
    $day = substr($str, 6, 2);
    $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    return $months[(int)$month - 1] . ' ' . (int)$day . ', ' . $year;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消