Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a date in french format in a mysql datetime format

Tags:

date

php

datetime

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.

like image 738
Beno Avatar asked Nov 08 '11 14:11

Beno


4 Answers

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.

like image 69
Derek Downey Avatar answered Sep 21 '22 05:09

Derek Downey


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.

like image 38
Hugo Gresse Avatar answered Sep 21 '22 05:09

Hugo Gresse


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);
    }

}

}

like image 25
ZeroCool Avatar answered Sep 22 '22 05:09

ZeroCool


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

like image 27
azerttyu Avatar answered Sep 19 '22 05:09

azerttyu