Gabriel Meono
Gabriel Meono

Reputation: 1010

Issue replacing content with str_replace

I'm working with a CMS spanish website, and I'm trying to replace the months to spanish.

This is how it looks like with the date function date("F j, Y, g:i a"):

August 24, 2011, 1:47 pm

Now I want it to look like this:

Agosto 24, 2011, 1:47 pm

Using an example from the Php Documentation I made this:

$p['time'] = date("F j, Y, g:i a");
        $time_english = $p['time'];
        $search  = $time_english('August', 'September', 'October', 'November', 'December');
        $replace = $times_spanish('Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
        $subject ='August';
        str_replace($search, $replace, $subject);

The following error appears:

Fatal error: Call to undefined function August 24, 2011, 3:50 pm() in
    $search  = $time_english('August', 'September', 'October', 'November', 'December');

Upvotes: 1

Views: 674

Answers (3)

Wiseguy
Wiseguy

Reputation: 20873

I think you mean

    $search  = array('August', 'September', 'October', 'November', 'December');
    $replace = array('Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');

instead of

    $search  = $time_english('August', 'September', 'October', 'November', 'December');
    $replace = $times_spanish('Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');

As a point of interest, the reason your error says it's trying to call a function named August 24, 2011, 3:50 pm() is because of the apparent variable function name $time_english(). It's returning the value of $time_english then trying to run that as a function.


Here's the whole thing:

$p['time'] = date("F j, Y, g:i a");
$time_english = $p['time'];
$search  = array('August', 'September', 'October', 'November', 'December');
$replace = array('Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
$time_spanish = str_replace($search, $replace, $time_english);

Upvotes: 1

jeroen
jeroen

Reputation: 91792

It would be far easier to just use strftime(). All you have to do is set a locale and you can output in your desired language.

Example:

setlocale(LC_ALL, 'es_ES');    // I think it´s es_ES
$my_time = strftime("%B %e, %G, %I:%M %P");    // something like that...

Upvotes: 2

genesis
genesis

Reputation: 50982

This is probably correct one

$p['time'] = date("F j, Y, g:i a");

$search  = array('August', 'September', 'October', 'November', 'December');
$replace = array('Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');
str_replace($search, $replace, $subject);

Upvotes: 1

Related Questions