I am trying to save dates to an SQL-database using Doctrine.
The user writes the dates to input-fields using datepicker and they have the format dd.mm.yyyy (german). In the database the dates look like this "2021-09-30"
or this "0000-00-00"
if no date is given
I am using this code to set the Input data to the entity:
if(isset($formData["startDate"]) and $formData["startDate"] != "") {
$sD = explode(".", $formData["startDate"]);
$campaign->setStartDate(($sD[2]."-".$sD[1]."-".$sD[0]));
} else {
$campaign->setStartDate("0000-00-00")
}
But this produces the error Fatal error: Call to a member function format() on string
.
How can i save the dates to my database?
Here are the field and getter/setter:
/**
* @ORM\Column(type="date")
*/
protected $startDate;
/**
* Set startDate
*
* @param integer $startDate
* @return Campaign
*/
public function setStartDate($startDate)
{
$this->startDate = $startDate;
return $this;
}
/**
* Get startDate
*
* @return integer
*/
public function getStartDate()
{
return $this->startDate;
}