Error trying to convert Portuguese date into datetime

8

I have a problem and I can not find a solution to fix it. I have a date in this format: Wednesday, April 30, 2014 and I want to save this date in the database in type datetime .

So I am trying to convert to type datetime , but I have a problem:

If the date is in English : Wednesday, 30 April, 2014 so my echo works, and shows the date in date time.

If the date is in Portuguese: Quarta-feira, 30 Abril, 2014 echo no longer works and I get this error: Call to a member function format() on a non-object .

I'm doing the conversion with the class DateTime ::

$data= DateTime::createFromFormat('l, j F, Y', 'Quarta-feira, 30 Abril, 2014');
echo $data->format('Y-m-d');

Has anyone ever had this problem and know how I can solve it?

    
asked by anonymous 30.04.2014 / 17:50

1 answer

4
function dataPT($data){
        $date = DateTime::createFromFormat('Ymd', $data);
        $day    = $date->format("l");
        $daynum = $date->format("j");
        $month  = $date->format("F");
        $year   = $date->format("Y");

        switch($day)
        {
            case "Monday":    $day = "Segunda-Feira";  break;
            case "Tuesday":   $day = "Terça-Feira"; break;
            case "Wednesday": $day = "Quarta-Feira";  break;
            case "Thursday":  $day = "Quinta-Feira"; break;
            case "Friday":    $day = "Sexta-Feira";  break;
            case "Saturday":  $day = "Sábado";  break;
            case "Sunday":    $day = "Domingo";  break;
            default:          $day = "Unknown"; break;
        }

        switch($month)
        {
            case "January":   $month = "Janeiro";    break;
            case "February":  $month = "Fevereiro";   break;
            case "March":     $month = "Março";     break;
            case "April":     $month = "Abril";     break;
            case "May":       $month = "Maio";       break;
            case "June":      $month = "Junho";      break;
            case "July":      $month = "Julho";      break;
            case "August":    $month = "Agosto";    break;
            case "September": $month = "Setembro"; break;
            case "October":   $month = "Outubro";   break;
            case "November":  $month = "Novembro";  break;
            case "December":  $month = "Dezembro";  break;
            default:          $month = "Unknown";   break;
        }

    echo $daynum . " de " . $month . " de " . $year;
}

Here you get the dates in English and change to Portuguese, you can change the function from Portuguese to English and perform the conversion before executing the function.

    
30.04.2014 / 18:01