

[PHP] Convert a date from french month name to another format
source link: https://gist.github.com/rseon/7d39cd3d3503477505c5239c47439e8d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

<?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); }
Recommend
-
5
Technical Articles
-
18
SQL Trivia – How to convert milliseconds to [hh:mm:ss.ms] format Today for some reporting purpose I need to convert milliseconds to hh:mm:ss.ms format, i.e. Hours : Minutes : Seconds . Micro-Seconds
-
15
PowerShell script to convert CSV file format to Complex Nested JSON by Kapil Khandelwal · August 11, 2021 · 10 Views Frequently, we get raw d...
-
14
How To Convert VirtualBox Disk Image (VDI) and img to Qcow2 formatThis simple guide will show you the easiest way to convert VDI disk image to use on your Openstack Cloud environ...
-
14
How to Convert Time in 12-Hour Format to 24-Hour Format With Programming By Yuvraj Chandra Published 7 hours ago Want 4:30 PM t...
-
7
Export/Convert Table or SQL Query data to JSON string format | SQL Server 2016 – Part 1 In my [previous post]
-
10
How to Convert ePub file to PDF Format on Linux CLIAre you looking for a way to convert an EPUB format file to a PDF file on the Linux command line interface?. EPUB is an e-book file format that uses the “.epub” file extensio...
-
6
Convert Variables and Strings to Date-Time Format advertisements I currently have the following variables and f...
-
15
How to Convert a Date from yyyy-mm-dd to dd-mm-yyyy Format in PHP 4697 views 1 year ago PHP Use the...
-
3
FORMAT is a convenient but expensive SQL Server function - Part 1 By: Aaron Bertrand | Updated: 2022-02-16 |
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK