Convert month number to month letter

0

I have this line of text to send a data from a column from my database and use the substr so that it only shows 01 of 2018-01-05T14: 00.

My question is how can I convert that 01 that shows me to month in letter and so on 02, 03, etc.

<h2><?php $mes = substr($fechamovimiento, 5, -9);?> <?php echo $mes;?></h2>
    
asked by christianfabela 03.01.2019 в 19:07
source

2 answers

2

In the example that I put create an array with the months, you can modify the words and show what you want:

<?php
        $fechamovimiento =  "2018-12-05T14:00";
        $meses = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
        $mes = substr($fechamovimiento, 5, -9);
        if ($mes <= 12) {
            echo $meses[$mes - 1];
        }
        else{
            echo "Solo existen 12 meses hay un error en el formato de tu fecha: ".$fechamovimiento;
        }

Basically what you do is create an array with the words, then as you already have the month separated from the date that is a number you go and you look for it within the array, subtracting one from it since if you had month 3 it would be March but in your array March is in position 2 because the array starts from 0, and if by error you get a month higher than 12 I put a message:

    
answered by 03.01.2019 / 19:33
source
1

@Juan's answer is correct. But if you are taking it from an SQL database you can surely make the base deliver the month in Spanish.
Here there is a response on how to do it in MySQL

    
answered by 03.01.2019 в 19:55