I have a simple problem into my code. I have a function where I pass a date into an italian format and I want to change the format to english.
I have try this:
In your case I would use a simple translation array to convert Italian month names to English month names. Something like this should work:-
/**
* @param $dateStr
* @return DateTime
*/
function getDateFromItalian($dateStr)
{
$translation = array(
'gennaio' => 'January',
'febbraio' => 'February',
'marzo' => 'March',
'aprile' => 'April',
'maggio' => 'May',
'giugno' => 'June',
'luglio' => 'July',
'agosto' => 'August',
'settembre' => 'September',
'ottobre' => 'October',
'novembre' => 'November',
'dicembre' => 'December',
'gen' => 'January',
'feb' => 'February',
'mar' => 'March',
'apr' => 'April',
'mag' => 'May',
'giu' => 'June',
'lug' => 'July',
'ago' => 'August',
'set' => 'September',
'ott' => 'October',
'nov' => 'November',
'dic' => 'December',
);
list($day, $month, $year) = explode('/', $dateStr);
return new \DateTime($day . ' ' . $translation[strtolower($month)] . ' ' . $year, new \DateTimeZone('Europe/Rome'));
}
var_dump(getDateFromItalian('01/lug/2013'));
Output:-
object(DateTime)[1]
public 'date' => string '2013-07-01 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Rome' (length=11)
see the PHP DateTime manual for more information.