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.
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.
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.
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);
}
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With