ronal vasquez ronal vasquez - 11 days ago 7
PHP Question

Date bad in php

i have this code:

$evt_start_date = date("m/d/Y", strtotime($event_date_from));


where:
$event_date_from = enero 15, 2017
this format date is in spanish.

the function strtotime is not work, because $event_date_from have incorrect format.

As you can correctly format this variable $event_Date_from before passing to the function strtotime.

How can I convert the date "ENERO 15, 2017" into a correct format so that the "srttotime" function does not return an error.

Answer

You can use the PHP DateTime object - http://php.net/manual/en/datetime.createfromformat.php

You will need to replace the month name with an english name first, for example,

$search = [
  'spanishJanuary',
  'spanishFebruary',
  'spanishMarch',
  // etc...
];

$replace = [
  'January',
  'February',
  'March',
  // etc...
];

$spanish_date_string = 'spanishMarch 15, 2017';

$date_string = str_replace($search, $replace, $spanish_date_string);

Once that's done you can use the DateTime object like this,

// $date_string now changed to "March 15, 2017"
$date = DateTime::createFromFormat('F j, Y', $date_string);
echo $date->format('m/d/Y');

Which will result in

03/15/2017