Skip to content

Instantly share code, notes, and snippets.

@rseon
Last active April 14, 2022 04:06
Show Gist options
  • Save rseon/7d39cd3d3503477505c5239c47439e8d to your computer and use it in GitHub Desktop.
Save rseon/7d39cd3d3503477505c5239c47439e8d to your computer and use it in GitHub Desktop.
[PHP] Convert a date from french month name to another format
<?php
/**
* Convert a date from french month name to another format
*
* @param string Date to convert
* @param string Format of date to convert
* @param string Format for output
* @return string
*
* @php_version >= 5.3
* @link http://php.net/manual/en/function.date.php All date formats
*/
function convert_date_fr($date, $format_in = 'j F Y', $format_out = 'Y-m-d') {
// French to english month names
$months = array(
'janvier' => 'january',
'février' => 'february',
'mars' => 'march',
'avril' => 'april',
'mai' => 'may',
'juin' => 'june',
'juillet' => 'july',
'août' => 'august',
'septembre' => 'september',
'octobre' => 'october',
'novembre' => 'november',
'décembre' => 'december',
);
// List of available formats for date
$formats_list = array('d','D','j','l','N','S','w','z','S','W','M','F','m','M','n','t','A','L','o','Y','y','H','a','A','B','g','G','h','H','i','s','u','v','F','e','I','O','P','T','Z','D','c','r','U');
// We get separators between elements in $date, based on $format_in
$split = str_split($format_in);
$separators = array();
$_continue = false;
foreach($split as $k => $s) {
if($_continue) {
$_continue = false;
continue;
}
// For escaped chars (like "\h")
if($s == '\\' && isset($split[$k+1])) {
$separators[] = '\\' . $split[$k+1];
$_continue = true;
continue;
}
if(!in_array($s, $formats_list)) {
$separators[] = $s;
}
}
// Translate month name
$tmp = preg_split('/('.implode('|', array_map(function($v) {
if($v == '/') {
return '\/';
}
return str_replace('\\', '\\\\', $v);
}, $separators)).')/', $date);
foreach($tmp as $k => $v) {
$v = mb_strtolower($v, 'UTF-8');
if(isset($months[$v])) {
$tmp[$k] = $months[$v];
}
}
// Re-construct the date
$imploded = '';
foreach($tmp as $k => $v) {
$imploded .= $v . (isset($separators[$k]) ? str_replace('\\', '', $separators[$k]) : '');
}
return DateTime::createFromFormat($format_in, $imploded)->format($format_out);
}
<?php
echo convert_date_fr('5 février 2018'); // => 2018-02-05
echo convert_date_fr('8 Décembre', 'j F', 'd/m'); // => 08/12
echo convert_date_fr('8-août 2018 14:30:54', 'j-F Y H:i:s', 'Y-m-d H:i:s'); // => 2018-08-08 14:30:54
echo convert_date_fr('8 août \à 14\h30', 'j-F \à H\hi', 'Y-m-d H:i:s'); // => 2018-08-08 14:30:00
echo convert_date_fr('8 Février \à 14\h30', 'j-F \à H\hi', 'd/m \à H\hi'); // => 08/02 à 14h30
echo convert_date_fr('18/12/2018', 'd/m/Y', 'Y-m-d'); // => 2018-12-18
echo convert_date_fr('18/12/2018', 'd/m/Y', 'j F Y'); // => 18 December 2018
echo convert_date_fr('8 Février à 14h30', 'j-F \à H\hi', 'd/m \à H\hi'); // => fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment