In PHP, how to convert a date in French?

Function dateToFrench

The following PHP function can be used to convert any date into French. The first parameter is the date, the second is the expected format. The array $english_days and $french_days contain name of days respectively in English and French. The function use str_replace to replace English name of days by French ones. The same principle is used for months.

// Convert a date or timestamp into French.
public static function dateToFrench($date, $format) 
{
    $english_days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
    $french_days = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
    $english_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    $french_months = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');
    return str_replace($english_months, $french_months, str_replace($english_days, $french_days, date($format, strtotime($date) ) ) );
}

Examples

The first parameter is similar to the strtotime() function first parameter. The second parameter is the expected format, similar to the date() function. Here are some example:

See also


Last update : 07/09/2018