0

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;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
lxg95
  • 553
  • 2
  • 8
  • 28
  • 1
    Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – jrswgtr Oct 05 '21 at 13:26
  • Not at all, as the error is not produced by me directly, but occurs later in DateType.php – lxg95 Oct 05 '21 at 13:30
  • I am pretty sure i have to turn the string into some dateformat, but everything i am trying, like strtotime() doesn't do the trick either. – lxg95 Oct 05 '21 at 13:37
  • `format()` is a function which is available on the PHP [DateTime](https://www.php.net/manual/en/class.datetime.php) object, so you're probably meant to supply one of those - it makes sense, given the context. If you're getting an input string in a non-standard format such as the one you described, then use the static function [createFromFormat](https://www.php.net/manual/en/datetime.createfromformat.php) to create a DateTime object from the input data. – ADyson Oct 05 '21 at 14:17
  • If you want it gone, you can request it by flagging. Duplicates aren't a bad thing though. – General Grievance Jan 17 '23 at 21:04

1 Answers1

1

First of all, you can define your setter/getter with \DateTimeInterface, like this

/**
 * @ORM\Column(type="datetime", options={"default":"0000-00-00 00:00:00"})
 */
 protected $startDate;

/**
 * Set startDate
 *
 * @param \DateTimeInterface $startDate
 * @return Campaign
 */
public function setStartDate(\DateTimeInterface $startDate)
{
    $this->startDate = $startDate;

    return $this;
}

/**
 * Get startDate
 *
 * @return \DateTimeInterface
 */
public function getStartDate()
{
    return $this->startDate;
}

Then, you should have a better output. Note that 0000-00-00 00:00:00 is an invalid date...

For your error, you can make it like this

if(isset($formData["startDate"]) && !empty($formData["startDate"])) {
    $date = DateTime::createFromFormat('Y\.m\.d', $formData["startDate"]);
} else {
    $date = new DateTime('now');
}

$campaign->setStartDate($date);

Hope it's help

  • This is really nice, thank you. $date = new DateTime('now'); is really good for the startDate, but i am doing the same thing with endDate as well. What would i do if i don't want to have an endDate for an Entity? – lxg95 Oct 06 '21 at 11:17