php format date [duplicate]

0

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

$mostrarso=mysql_query($so);
				  while($i=mysql_fetch_array($mostrarso)){
            $fecha_mes=$i[1];
$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
source

2 answers

0

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

example:

$nombremes=date("F",strtotime($fecha_mes));

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

   date_default_timezone_set('Europe/Madrid');
   setlocale(LC_TIME, 'es_ES.UTF-8');
   setlocale(LC_TIME, 'spanish');

   $mostrarso=mysql_query($so);
   while($i=mysql_fetch_array($mostrarso)){
      $fecha_mes=$i[1];
      $nombre_mes=strftime("%B", strtotime($fecha_mes));
      $day=date("d",strtotime($fecha_mes));
      $year=date("Y",strtotime($fecha_mes));
      $_fecha = "{$day}-{$nombre_mes}-{$year} ";
      echo $nombre_mes;
   }
    
answered by 16.11.2018 / 01:41
source
0

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.
 */
while($i=mysql_fetch_array($mostrarso)){
    $fecha=new DateTime($i[1]);
    $numMes=$fecha->format('m');
    $nombreMes=$esMeses[$numMes-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