Change an int type value to one string type PHP

0

I make a query to the DB and I get the value "month" of the records of the table that is an integer type.

I want that according to the month received, PHP will print it in its string form. e.g. (month = 1 then print it as month="January").

Here I leave the code:

$peticion="SELECT * FROM operaciones WHERE anio='2016' AND suc_numerosede='1'";
$consulta=mysqli_query($conexion,$peticion);
$i=1;
while ($fila=mysqli_fetch_array($consulta)) {
  echo $mes[$i]=$fila['mes'].'<br />';
  $i++;
}

This is because I want to print the monthly records, but the months appear string and not integers.

    
asked by Ronel Jimenez 02.03.2017 в 16:44
source

5 answers

3

If the numerical value of the date is known, another way of doing it is by using the createFromFormat of the class DateTime .

For example if the month = 3 ...

$mes_num  = 3;
echo nombre_mes($mes_num);

You only have to call the function nombre_mes() within the loop, passing as a parameter the number of the month retrieved from the database.

function nombre_mes($mes_num){

   $obj_fecha = DateTime::createFromFormat('!m', $mes_num,new DateTimeZone('Europe/Madrid'));
   $mes_letra=$obj_fecha->format('F');
   return $mes_letra;
}

VIEW DEMO

Another way to do it: Combining the functions date() and mktime() :

$mes_num  = 6;
$mes_nombre = date('F', mktime(0, 0, 0, $mes_num, 10)); // Junio

Explanation of the function:

  • !m , we are restarting the month field to the Unix Era (see link of createFromFormat above).
  • F to get the full name of the month (if we change it by M it will show only the first three letters of the same.

NOTE:
  

Attention, your query is susceptible to SQL injection . He   Recommend using always prepared queries.

    
answered by 02.03.2017 в 17:46
3

Define a function:

 function getMes($n) {
     if ($n==1) return "enero";
     if ($n==2) return "febrero";
     if ($n==3) return "marzo";
     if ($n==4) return "abril";
     if ($n==5) return "mayo";
     if ($n==6) return "junio";
     if ($n==7) return "julio";
     if ($n==8) return "agosto";
     if ($n==9) return "septiembre";
     if ($n==10) return "octubre";
     if ($n==11) return "noviembre";
     if ($n==12) return "diciembre";
 }

another alternative to that function:

 function getMes($n) {
     $meses = array("enero","febrero","marzo","abril","mayo","junio","julio"
     "agosto","septiembre","octubre","noviembre","diciembre");
     return $meses[$n-1];
 }

and when you print:

echo getMes($fila['mes']).'<br />';
    
answered by 02.03.2017 в 17:03
2

Try strval ()

Example:

$items = strval($var); // $items === "5";
    
answered by 02.03.2017 в 16:51
1

The most direct approach to this is through nested ternary operations :

while ($fila=mysqli_fetch_array($consulta)) {
  $strMes = 1 == $fila['mes'] ? "enero" : 2 == $fila['mes'] ? "febrero" : 3 == $fila['mes'] ? "marzo" : 4 == $fila['mes'] ? "abril" : 5 == $fila['mes'] ? "mayo" : 6 == $fila['mes'] ? "junio" : 7 == $fila['mes'] ? "julio" : 8 == $fila['mes'] ? "agosto" : 9 == $fila['mes'] ? "septiembre" : 10 == $fila['mes'] ? "octubre" : 11 == $fila['mes'] ? "noviembre" : "diciembre";
  echo $strMes.'<br />';
  $i++;
}
    
answered by 02.03.2017 в 16:52
1
$mes = date ("F",1);
print_r($mes); // January

As simple as this.

 setlocale(LC_ALL,"");
 echo strftime("%B",1); //Enero
  

Note:    In Windows, setlocale (LC_ALL, '') sets the    localism names from the regional or system language settings   (accessible through the Control Panel).

Source

Another source

    
answered by 02.03.2017 в 20:55