Convert php date

3

I have the following date: "11-07-2017" day / month / year

I need to convert it to "yy / mm / dd" to insert it into the mysql bd I have tried with this without results:

$date = 11-07-2017;
date_format($date,'Y-m-d');

I get this error:

  

Severity: Warning

     

Message: date_format () expects parameter 1 to be DateTime, string   given

    
asked by Javier Antonio Aguayo Aguilar 03.07.2017 в 23:06
source

1 answer

4

As the error itself indicates, you are passing it a String as a parameter when you should pass it a date object.

To do this, you should use the DateTime function:

<?php
    $date = new DateTime("11-07-2017");
    $fecha = date_format($date,'Y-m-d');
    echo $fecha; //Devuelve 2017-07-11

If you wanted the separations to be with bars then you should put:

$fecha = date_format($date,'Y/m/d');
echo $fecha; //Devuelve 2017/07/11
    
answered by 03.07.2017 / 23:10
source