1

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:

public function changeDateFormat($date_start_old){
        $date_start_old = strtotime($date_start_old);
        $date_start = date("Y/m/d" , $date_start_old);

        echo('Date start: '.$date_start);
}

My old date_start is:

01/lug/2013

(is equivalent of 01/07/2013)

And when I try to print new date_start I retrieve this because doesn't recognize lug:

1970/01/01 

Is there a way to transform italian date to english and after change format?

Thanks

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171

1 Answers1

2

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.

vascowhite
  • 18,120
  • 9
  • 61
  • 77