Convert a date in french format in a mysql datetime format
Solution 1
How about?
date("Y-m-d H:i:s", strtotime($datetime));
Ah, yes, I see the problem. strtotime
only converts english text (emphasis mine):
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp
Your best bet is probably going to be preg_match
for your specific format, as there doesn't seem to be any locale-specific functions that will convert things like the month name.
Solution 2
I just wrote this to convert a french date in day/months/year to english format and next to MYSQL. Assuming original date is separated by a "/" slash
private function dateToMySQL($date){
$tabDate = explode('/' , $date);
$date = $tabDate[2].'-'.$tabDate[1].'-'.$tabDate[0];
$date = date( 'Y-m-d H:i:s', strtotime($date) );
return $date;
}
It's pretty basic and warn if missing numbers like day.
Solution 3
I know question was long a time, but as I've just had same probleme, I propose this solution :
DateTime::createFromFormat('d/m/Y H:i',$datetime)->format('Y-m-d H:i')
But PHP 5.3 required. More information on http://php.net/manual/en/datetime.createfromformat.php
Solution 4
if you have array or multiArray i made this function, juste all you date input start 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);
}
}
}
Solution 5
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 the mysql format use this function :
date('Y-m-d',strtotime(frToEn($date))
Beno
Updated on June 23, 2022Comments
-
Beno almost 2 years
I'd like to convert a date in french format (08 novembre 2011 à 06h00) in a datetime format (2011-11-08 06:00).
I'm able to convert a datetime in french format date with a little function:
function dateFR($datetime) { setlocale(LC_ALL, 'fr_FR'); return strftime('%d %B %Y à %Hh%M', strtotime($datetime)); }
But I don't know how to do the opposite.
Thank you for your help.