Convert date in french format to mysql datetime format

I want to convert the date in French format (November 08, 2011 to 06h00) in the date and time format (2011-11-08 06:00).

I can convert the date-time in French format with a little function:

function dateFR($datetime) { setlocale(LC_ALL, 'fr_FR'); return strftime('%d %B %Y à %Hh%M', strtotime($datetime)); } 

But I do not know how to do the opposite.

Thank you for your help.

+5
source share
5 answers

What about?

 date("Ymd H:i:s", strtotime($datetime)); 

Oh yes, I see a problem. strtotime only converts English text (emphasis mine):

The function expects that a string containing the English date format will be set, and will try to parse this format in the Unix timestamp

Your best bet is likely to be preg_match for your specific format, as there does not seem to be any locale-specific features that will convert things like month name.

+6
source

I just wrote this to convert a French date in day / months / year format to English and next to MYSQL. Assuming the original date is separated by a "/" slash

  private function dateToMySQL($date){ $tabDate = explode('/' , $date); $date = $tabDate[2].'-'.$tabDate[1].'-'.$tabDate[0]; $date = date( 'Ymd H:i:s', strtotime($date) ); return $date; } 

This is quite simple and warns if there are no rooms, for example, a day.

+3
source

if you have an array or multiArray i made this function, juste everything you enter starts with name = "date _...."

 function dateFormatSql(&$array){ foreach ($array as $key=>&$value){ if(!is_array($value)){ if(preg_match("#^date_#", $key)){ $tabDate = explode('/' , $value); $date = $tabDate[2].'/'.$tabDate[1].'/'.$tabDate[0]; $sqldate = date('Y/m/d', strtotime($date)); $array[$key] = $sqldate; } }else{ dateFormatSql($value); } } 

}

+1
source

I know the question was long, but since I had the same problem, I propose this solution:

 DateTime::createFromFormat('d/m/YH:i',$datetime)->format('Ymd H:i') 

But PHP 5.3 is required. Additional information about http://php.net/manual/en/datetime.createfromformat.php

0
source

try this code:

 function frToEn($date){ /* this function Convert a date in french format to english format ** EXP : ** French date : 20-juin-2019 ** English date : 20-jun-2019 */ $month=substr($s1=substr($date,strpos($date,'-')+1) , 0 , -(strlen($s1)-strpos($s1,"-"))); $year=substr($s1,strpos($s1,'-')+1); $day=substr($date,0,strpos($date,'-')); switch ($month) { case 'janvier': case 'janv': case 'jan': return $day."-jan-".$year; break; case 'février' : case'févr' : case'fév': return $day."-feb-".$year; break; case 'mars' : case'mar': return $day."-mar-".$year; break; case 'avril' : case'avr': return $day."-apr-".$year; break; case 'mai': return $day."-may-".$year; break; case 'juin': return $day."-jun-".$year; break; case 'juillet' : case'juil': return $day."-jul-".$year; break; case 'aout' : case'août': return $day."-aug-".$year; break; case 'septembre' : case'sept' : case'sep': return $day."-sep-".$year; break; case 'octobre' : case'oct': return $day."-oct-".$year; break; case 'novembre' : case'nov': return $day."-nov-".$year; break; case 'décembre' : case'déc': return $day."-dec-".$year; break; default: return false; break; }} 

for mysql format use this function:

 date('Ym-d',strtotime(frToEn($date)) 
0
source

Source: https://habr.com/ru/post/1380202/


All Articles