PHP: Format Date, and then convert Date to String

0

(I indicate with numbers and letters so that the question is better understood)

I made a form in html that sends a date:

<input type="date"> //(no DateTime)

then php has to "Format" this Date and "Convert" it to String to make a mysql query.

The problem I have is that:

  

(0) The date that I am receiving is of type "Date"?

     

(1) The Date is in the format 2017-10-09 and I want to format it in 2017/10/09

     

(2) This Date I want to convert it to String (to later use it in the mysql query)

(A) I tried to solve it in several ways, but all resulted in error:

$myDate = date_format($_POST['fechaDesde'],"Y/m/d"); //(1)

$fechaDesde = $myDate->format('Y/m/d');              //(2)

echo $fechaDesde; //debería tener String: 2017/10/09

(B) Another failed attempt:

$myDate = $_POST['fechaDesde']; 

$fecha = Date::createFromFormat('Y/m/d', $myDate);   //(1)

$fechaDesde = $fecha->format('Y/m/d');               //(2)

echo $fechaDesde; //debería tener String: 2017/10/09
    
asked by Nicolas Retamar 20.10.2017 в 15:26
source

1 answer

0

You can use strtotime to convert the date to time and then to date format it as you wish.

date('Y/d/m',strtotime('2017-10-09'));

date('Y/d/m',strtotime($_POST['fechaDesde']));

Greetings

    
answered by 20.10.2017 в 15:42