Error trying to convert Portuguese date into datetime

Asked

Viewed 696 times

8

I’m in trouble and I’m not finding any solution to fix it. I have a date in this format: Wednesday, 30 April, 2014 and I want to save this date in the database on type datetime.

So I’m trying to make the conversion for the guy datetime, but I have a problem:

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

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

I’m doing the conversion with the Datetime class::

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

Someone’s had this problem before and you know how I can fix it?

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 receive the dates in English and change to Portuguese, you can change the function to Portuguese to English and perform the conversion before performing the function.

  • Thank you very much, finally problem solved!! :)

  • After all, I looked at the code and it looked like what I was looking for, but now that I’ve tested it, I’m making the same mistake!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.