Show date field information - php symfony

Asked

Viewed 125 times

2

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

2009-09-10. 

I’m trying to bring them via the symfony json this way:

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

in my field customization file is 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’ve researched and found nothing to help me.

2 answers

2

I’ll show you a function that I use here that converts from English to Portuguese date format

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 the include on the page I’ll need

$n_dt   =   new datas();

Then I take the date I want and usually put in a variable so I don’t have to use the function every single time

    $dt_nascimento = $n_dt->ajusta_data('/','-',$_POST['dt_nascimento']);

0

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

I mean, instead of doing this:

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

... do this:

$tbdeliberacoes->getData()->format('d/m/Y');

Browser other questions tagged

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