php format date [duplicate]


Hello, I have doubts as to how I can pass the month from number to name before I had my date on date (Y-m-d) I wanted to print the month and can divide it in parts with this

$partes = explode('-', $fecha_mes);
$_fecha = "{$partes[2]}-{$partes[1]}-{$partes[0]} ";

I print it this way

<td><?php echo   $partes[1]  ;?></td>

Now I want that value of the month instead of number that is the name of the month like January, February etc.

asked by Esther 15.11.2018 в 23:48

2 answers


You can convert your $ date_month to date with the strtotime function and then only get the month with the date function



you can add setlocale (LC_TIME, 'es_ES', 'esp_esp'); so I can change it to Spanish

another option would be with the strftime function formatted in Spanish

   setlocale(LC_TIME, 'es_ES.UTF-8');
   setlocale(LC_TIME, 'spanish');

      $nombre_mes=strftime("%B", strtotime($fecha_mes));
      $_fecha = "{$day}-{$nombre_mes}-{$year} ";
      echo $nombre_mes;
answered by 16.11.2018 / 01:41

For me it has never been possible to show the name of the month in Spanish using setlocale . In my case I speak of a web page hosted on a server in English.

As an alternative you can do something like this:

/*Un array con los nombres de los meses*/
$esMeses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");

   *Vamos a crear a partir de tu fecha un verdadero objeto DateTime
   *y a sacar partida de él, obteniendo la representación numérica del mes
   *usando ->format('m')
   *luego buscaremos el valor de ese índice en $esMeses
   *Aquí [$numMes-1] restamos uno para aprovechar los índices naturales
   *del array: Enero sería 0, Febrero 1, etc, por eso restamos 1.
    $fecha=new DateTime($i[1]);
    echo "<td>$nombreMes</td>";

If you plan to use this much or do another type of translations , for example of the day, etc, consider using a utilitarian class similar to this .

answered by 16.11.2018 в 04:46