错误:在生成每一年的非对象上调用成员函数format()

时间:2016-02-11 07:04:16

标签: php postgresql symfony datetime strtotime

我有一个日历类我输入的年份仅来自用户并生成一年中的全天并保存在数据库中。

class Calendar
{
   /**
     * @var \DateTime
     *
     * @ORM\Column(name="today_date", type="datetime", nullable=true)
     */
    protected $todayDate;
    /**
     * @var boolean $isBusinessDay
     * @ORM\Column(name="is_business_day", type="boolean", nullable=true)
     */
    protected $isBusinessDay;
    /**
     * @var boolean $isHoliday
     * @ORM\Column(name="is_holiday", type="boolean", nullable=true)
     */
    protected $isHoliday;
    /**
     * @var time $openTime
     *
     * @ORM\Column(name="open_time", type="time", nullable=true)
     */
    protected $openTime;
    /**
     * @var time $openTime
     *
     * @ORM\Column(name="close_time", type="time", nullable=true)
     */
    protected $closeTime;

}

if ($form->isSubmitted() && $form->isValid()) { 
  $weekday = $form["weekday"]->getData();
  $start_date = $form["todayDate"]->getData();
  $start_date = (string) $start_date->format('Y-m-d');
  $start_day = date('z', strtotime($start_date));
  $days_in_a_year = date('z', strtotime('2016-12-31'));  
  $number_of_days = ($days_in_a_year - $start_day) +1 ;
  for ($i = 0; $i < $number_of_days; $i++) {
      $date = strtotime(date("Y-m-d", strtotime($start_date)) . " +$i day");
      print date('d F - l', $date) .'<br />';
      if (in_array(date('l', $date), $weekday))
      {
        print "Match found".'<br />';
        $date_temp2 = date('Y-m-d',$date);
        print $date_temp2.'<br />';
        $date_temp = new \DateTime($date_temp2);
        $TodayDate = $date_temp->format('Y-m-d');
        $calendar2 = new Calendar();
        $calendar2->setTodayDate($TodayDate);
        $calendar2->setOpenTime($form["openTime"]->getData());
        $calendar2->setCloseTime($form["closeTime"]->getData());
        $calendar2->setIsBusinessDay(true);
        $calendar2->setIsHoliday(false);
        $em->persist($calendar2);
      }
      else
      {
        print "Match not found".'<br />';
        $calendar2 = new Calendar();
        $calendar2->setIsBusinessDay(false);
        $calendar2->setIsHoliday(true);
        $em->persist($calendar2);
       }
      }
    $em->flush(); 
   }

其实我想从用户那里获得年份。获取年份的第一天并在数据库中存储添加一个增量日。 在If Block中,它打印全年的日期。到

30 December - Friday
Match found
2016-12-30
31 December - Saturday
Match not found

最后我收到了这个错误

  

错误:在非对象上调用成员函数format()

1 个答案:

答案 0 :(得分:0)

来自>nohup.out nohup ./example.sh & 的{​​{1}}肯定是$start_date

添加支票以防止此错误。

更改:

$form['todayDate']

致:

null

像这样,您将当前日期视为$start_date = $form['todayDate']; 对象,并且可以使用$start_date = $form['todayDate'] ?: new \DateTime();