Display field information date - php symfony

2

I have a table in the database with data of type date, the dates in the database are in this format:

2009-09-10. 

I'm trying to get them via json from symfony like this:

linha 7: date("d/m/Y", strtotime($tbdeliberacoes->getData()))

In my field customization file it looks like this:

 $this->widgetSchema['data'] = new sfWidgetFormDate(array('label' => 'Data'));

But it is this error that does not let the data appear on the screen:

Warning: strtotime() expects parameter 1 to be string, object given in line 7

I already researched and found nothing that would help me.

    
asked by anonymous 14.08.2015 / 20:05

2 answers

2

I'll show you a function that I use here that does this conversion from English to Portuguese format of dates

Class datas {
public function ajusta_data($char1,$char2,$dt) {

    if ($char1 == "-") {
        if ($dt < "1900-01-01 00:00:00" OR $dt > "2100-12-31 23:59:59") {
            $ret = "";
            return $ret;
        }
        $dia = 2;
    } else {
        $dia = 0;
    }

    $temp = explode($char1,substr($dt,0,10));
    if ($temp[1] == '02' AND $temp[$dia] > 28) {$temp[$dia] = 28;}
    if (($temp[1] == '04' OR $temp[1] == '06' OR $temp[1] == '09' OR $temp[1] == '11') AND $temp[$dia] > 30) {$temp[$dia] = 30;}
    if ($temp[0] != "0000" AND $temp[0] != "") $dt  = $temp[2].$char2.$temp[1].$char2.$temp[0].substr($dt,10,10);

    if ($char2 == "-") {
        if ($dt < "1900-01-01 00:00:00" OR $dt > "2100-12-31 23:59:59") {
            $ret = "";
            return $ret;
        }
    }

    return $dt;
  }
}

Then I'll use it, use the include on the page I'm going to need

$n_dt   =   new datas();

Then I get the date I want and I usually put it in a variable so I do not have to use the whole function instead.

    $dt_nascimento = $n_dt->ajusta_data('/','-',$_POST['dt_nascimento']);
    
14.08.2015 / 20:31
0

The error occurs because the contents of $tbdeliberacoes->getData() is an object of type \DateTime , not an integer (in this case, the date in UNIX format). Just use the format method to display the date in the format you want.

That is, instead of doing this:

date("d/m/Y", strtotime($tbdeliberacoes->getData()));

... do this:

$tbdeliberacoes->getData()->format('d/m/Y');
    
14.08.2015 / 21:45