phpdatereplacelocale

Create datetime expression using Spanish month


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

With str_replace function the string gets replaced, but the day and hour are missing.

Agosto

This is the code I'm using to define it:

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

    $echo = $time;

Solution

  • Try using setlocale to specify the correct language before calling date. E.g.

    /* Set locale to Spanish */
    setlocale(LC_ALL, 'es_ES');
    

    P.S. In your original code, using that approach, you need to replace:

    $time = str_replace($search, $replace, $subject);
    

    with

    $time = str_replace($search, $replace, $time);
    

    But I would recommend using setlocale so you don't have to worry about doing this yourself. Also,

    $echo = $time;
    

    is totally wrong; it should be:

    echo $time;