This question has already been answered:

The date comes in the format 01/05/2016. How to convert it to a type format 2016-01-05 using PHP

Reported as a duplicate by Saidolim , Abyx , user194374, Qwertiy , tutankhamun Jan 28 '16 at 21:01 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    @Saidolim The "duplicate" you specified is about working with the date FROM mysql in Java. And here from php In mysql ... - Mike
  • one
    Are you seriously? Search is not used? - korytoff

2 answers 2

If you are sure that the input date is correct and its control is not needed, then:

$date="05.01.2016"; $my_date=preg_replace("/(\d\d)\\.(\d\d)\\.(\d{4})/","$3-$2-$1",$date); echo $my_date; 

Or

 $date="05.01.2016"; $arr=explode(".",$date); $my_date=strftime("%F",mktime(0,0,0,$arr[1],$arr[0],$arr[2])); echo $my_date; 
  • thanks helped)) you need to learn regulars - duddeniska

If the question is only in the date conversion, and not how to store it in MySQL, then the shortest option:

date ('Ym-d', strtotime ('05 .01.2015 '));

  • one
    IMHO this option is better - jekaby
  • I'm only afraid of strtotime depends on the date settings in php.ini and can give unexpected results when moving to another hosting service, for example - Mike
  • @Mike has not yet encountered the strange behavior of this function, how much I use it [ php.net/manual/ru/… Замечание: Даты в формате m/d/y или dmy разрешают неоднозначность с помощью анализа разделителей их элементов: если разделителем является слеш (/), то дата интерпретируется в американском формате m/d/y, если же разделителем является дефис (-) или точка (.), то подразумевается использование европейского форматаd-my. - Maxim Tronenko