Change date language captured in RSS

Asked

Viewed 78 times

2

I’m taking the RSS content from a site 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 that the date of the content is and English, how do I change to Portuguese? The variable that captures the date is the $date.

2 answers

2


Hi @Felipe-Stoker.

Try to use the function strftime() 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: http://wbruno.com.br/php/imprimir-data-atual-em-portugues-php/

PHP: strftime - Manual

  • I did what you said, but it’s still the same. http://strangerthings.com.br/test/

  • Felipe, here it worked right, both on the local server, as on an external one: http://phpfiddle.org/main/code/79fd-931x. It may be that some configuration is missing in your php. has a look at this thread: http://stackoverflow.com/questions/10909911/php-setlocale-has-no-effect

  • 1

    It was the server yes.

1

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

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

for

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.