Separate date date format (Y-m-d)


Hello I have this date date format ("Y-m-d") I want to separate them in parts and print only my month of that date. How do I separate it?

$fecha=date("Y-m-d ");
asked by Esther 15.11.2018 в 20:42

3 answers


I would recommend that you work using Object Oriented Programming, using the class DateTime .

If in $fecha you receive a date like this for example: 2018-11-15 :

$objFecha = new DateTime($fecha, new DateTimeZone('America/Mexico_City'));
$mes= $objFecha->format('m');

echo $mes;

The output there would be 11 .


@Esther The solution that you comment here can be improved. There is no need to dizzy the data, making explode with them.

    $partes = explode('-', $fecha_mes); 
    $_fecha = "{$partes[2]}-{$partes[1]}-{$partes[0]}"; 
    <td><?php echo $partes[1];?> </td>

If you learn to work with objects you will save yourself a lot of headaches and many lines of code. For example:

    $fecha=new DateTime($i[1]); 
    <td><?php echo $fecha->format('m');?> </td>

And, if you want to show the name of the month in Spanish, you can use this utilitarian class , because with PHP per se, it is not possible to do it (if you are not interested in the whole class, adapt the month conversion function and nothing else or bring the array of months that the class uses and convert in situ ):

   *Dado que sería una clase propia debes incluirla
   *aquí suponemos que la clase está en un archivo llamado fecha_es.php
   *en el directorio classes
    $fecha=new FechaEs($i[1]); 
    <td><?php echo $fecha->getMMMM();?> </td>
answered by 15.11.2018 в 20:56

If you want to get the month, from a field type: Y-m-d that comes from your database I propose the following scenario

$fecha = "2018-11-25";

echo date('m', strtotime($fecha));

Where $fecha simulates being a value of type DATE that comes from my database; later with the function date() of php I pass as the first parameter the value I want to obtain in quotes, in this case it is the month, later use the function strtotime() to which I pass the name of the variable that contains the date that I want to manipulate

It should be clarified that:

  • If I use% co_of uppercase% I get the name of the month
  • If I use M minpuscula I get the number of the month
  • The function m serves and I quote according to

    (PHP 4, PHP 5, PHP 7)


    strtotime - Convert a textual date / time description into English   to a Unix date

    Link to official documentation: link

    So if there are several data you can do the following

    SELECT column1, column2, etc MONTH(columnaFecha) AS Mes FROM tu tabla;

    You use the function strtotime() to which inside the parentheses you pass the name of the column that has the date; that way they arrive formatted direct of the DB

    Here's the reference of that function in mysql

    answered by 15.11.2018 в 21:06

    You can do this

        $month = date("m");
        $year = date("y");

    For what you are going to occupy, only, concatenate it with .

    Try this ...

     $fecha = $_POST['fecha']; 
         $fecha=date("Y-m-d "); 
         $month = date("m");
             $year = date("y");
             $day = date("d");
        $partes = explode('-', $month); 
        $_fecha = "$year.$month.$day";
        <td><?php echo $month;?> </td>  
    answered by 15.11.2018 в 21:50