Change date language captured in RSS

2

I'm getting RSS content from a website with the following code:

<?php
$rss = new DOMDocument();
$rss->load('https://jovemnerd.com.br/nerdnews/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>'.$date.'</em></small></p>';
    echo '<p>'.$description.'</p>';
}?>

What happens, is the date of the content is English, how do I switch to Portuguese? The variable that captures the date is $ date .

    
asked by anonymous 10.11.2016 / 17:53

2 answers

2

Hello @ felipe-stoker.

Try to use the strftime() function instead of date() . Also, before the main execution, add the php locale functions to our region ( setlocale ).

Your code would look like this :

<?php

// loalidade BR e idioma pt_BR
setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");
date_default_timezone_set('America/Sao_Paulo');

$rss = new DOMDocument();
$rss->load('https://jovemnerd.com.br/nerdnews/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    // aqui foi alterado para a função com a formatação correta
    $date = strftime("%A, %d de %B de %Y", strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>'.$date.'</em></small></p>';
    echo '<p>'.$description.'</p>';
}

?>

References: link

PHP: strftime - Manual

    
10.11.2016 / 18:40
1

If strtotime($feed[$x]['date']) is ok and returning the correct timestamp, just swap:

$date = date('l F d, Y', strtotime($feed[$x]['date']));
// Thursday November 10, 2016

by

$date = strftime('%A, %e de %B de %Y', strtotime($feed[$x]['date']));
// Quinta-feira, 10 de novembro de 2016

The date() function does not accept internationalization, so use strftime()

    
10.11.2016 / 18:43