Converting String to DateFormat

102 Views Asked by At

I have this data | 01 Oktober 2017 |

This is an Indonesian date

And i want to convert into dateformat. And so the result must be 2017-10-01. Can you please help ?

2

There are 2 best solutions below

4
Sean Konig On

Use strtotime,

$date = strtotime($_POST['fixtureDate']);
    $fixtureDate = date("Y-m-j", $date);

You can then play with the format so that it displays how you want it to

0
Manav On

You need to convert dates with the month name in Indonesian into PHP readable date formats.

function getDate($indonesianMonthName){

  setlocale(LC_ALL, 'en_US');

  $month_numbers = range(1,12);

  foreach($month_numbers as $month)
    $english_months[] = strftime('%B',mktime(0,0,0,$month,1,2011));

  setlocale(LC_ALL, 'Indonesian');

  foreach($month_numbers as $month)
    $indonesian_months[] = strftime('%B',mktime(0,0,0,$month,1,2011));

  return str_replace($indonesian_months, $english_months, $indonesianMonthName);

}

echo getDate('01 Desember 2017');
echo getDate('01 Oktober 2017');

So basically, you create an array of every single month and its corresponding value in English and Indonesian, and then you use str_replace() to "translate" the date into English.

You can now use regulat strtotime() and Date() functions on the returned string.

References : https://stackoverflow.com/a/39874682/6124528