Iñaki Ledesma
Iñaki Ledesma

Reputation: 23

How to change date to spanish format with setlocale?

I trie to change the months name to spanish date but is not working. I don't know if I need to add a script in my head page. Here is my code:

<tbody>
    <?php
    setlocale(LC_ALL,"es_ES");
    for($i = 1; $i <=12; $i++):
      $dt = DateTime::createFromFormat('!m', $i);
      ?>
      <tr>
        <td><?= $dt->format("F");?></td>
        <td></td>
        <td></td>
      </tr>
    <?php endfor;?>
</tbody>

Upvotes: 0

Views: 4007

Answers (4)

Triplex
Triplex

Reputation: 1

As the @Agilefox user indicated, it's correct to do it like this:

setlocale(LC_TIME, 'es_ES', 'Spanish_Spain', 'Spanish');
$date = date('F j, Y');
echo strftime('%d %B %Y',strtotime($date));

_

But I would also like to indicate that it is useful to use es_ES.UTF-8 (if it work in the running actualy sistem), and in first position, as everyone already knows, according to the installed system, the first compatible option will be sought, and if es_ES.UTF-8 is compatible, it will apply it, since better results are obtained than with es_ES for some characters in spanish, leaving:

setlocale(LC_TIME, 'es_ES.UTF-8', 'es_ES', 'Spanish_Spain', 'Spanish');
$date = date('F j, Y');
echo strftime('%d %B %Y',strtotime($date));

Upvotes: 0

Juanma
Juanma

Reputation: 686

Try this code (see my comments for detailed explanation):

// Example date
$date = "2020-08-26"
// Set locale to Spanish
setlocale(LC_ALL, "es_ES.UTF-8");
// Transform the date to timestamp and then format to d-m-Y
$string = date("d-m-Y", strtotime($date));
// Create a new DateTimeObject
$date = \DateTime::createFromFormat("d-m-Y", $string);
// Get the timestamp from the previous object
// With strftime and the parameters (%B and %Y)
// %B - Full month name, based on the locale (agosto)
// %Y - Four digit representation for the year (2020)
// Returns a string (agosto 2020)
$date_humanized = strftime("%B %Y",$date->getTimestamp()); 

Upvotes: 0

Tuckbros
Tuckbros

Reputation: 437

First thing, you have to make sure the locale you want to use is available on your system. Then you have to use a function that uses the locale. At me (ubuntu desktop), I had to

sudo locale-gen es_ES
sudo update-locale 

To get something not english with the strftime function.

setlocale(LC_TIME,"es_ES");
for($i = 1; $i <=12; $i++){
      $dt = DateTime::createFromFormat('!m', $i);
      $time = mktime(0,0,0,$i);
    echo $dt->format("F") . " " . strftime('%B', $time) . " " . date('F', $time) . "\n";
}

Gives the following output :

January enero January
February febrero February
March marzo March
April abril April
May mayo May
June junio June
July julio July
August agosto August
September septiembre September
October octubre October
November noviembre November
December diciembre December

Upvotes: 1

AgileFox
AgileFox

Reputation: 174

Try this:

<?php
setlocale(LC_TIME, 'es_ES', 'Spanish_Spain', 'Spanish');
$date = date('F j, Y');
echo strftime('%d %B %Y',strtotime($date));

Upvotes: 4

Related Questions