ronal vasquez ronal vasquez - 1 year ago 88
PHP Question

Date bad in php

i have this code:

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

$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 Source

You can use the PHP DateTime object -

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

$search = [
  // etc...

$replace = [
  // 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download