@matveyvarg

Почему форматирование даты происход не корректно?

Имеется метод в контроллере, аналогичный date_format у smarty.
На вход подается строка вида "0000-00-00 00:00:00", однако что у метода, что у date_format возвращается строка "-0001-11-30", независимо от текущей даты.
Код метода:
(UserController)
private function _prepareUserDobToPostToForm($userData)
 {
        $userData['dob'] = Сonverter_DateTime::getInstance()->dbToFormDate($userData['dob']);
        return $userData;
}

(Converter/DateTime)
public function dbToFormDate($dateTime)
 {
        $this->_initDate($dateTime, $this->_dbTimezone);
        return $this->_dateToString(self::FORM_DATE_FORMAT, $this->_viewTimezone);
 }

 protected function _initDate($time, $timezone = null)
{
        if (null === $this->_date || (null !== $timezone && $this->_date->getTimezone()->getName() != $timezone)) {
            $this->_date = new DateTime('now', $timezone ? new DateTimeZone($timezone) : null);
        }
        if ($timezone) {
            $currentTimezone = date_default_timezone_get();
            if ($currentTimezone != $timezone) {
                date_default_timezone_set($this->_date->getTimezone()->getName());
            }
        }
        $this->_date->setTimestamp(null === $time ? time() : (is_int($time) ? $time : strtotime($time)));
        if (isset($currentTimezone)) {
            date_default_timezone_set($currentTimezone);
        }
}

protected function _dateToString($format, $timezone = null, $withShift = false)
{
        if ($timezone !== null) {

            if ($this->_date->getTimezone()->getName() != $timezone) {

                $this->_date->setTimezone(new DateTimeZone($timezone));
            }
        }
        $result = $this->_date->format($format);
        return $withShift ? $result . ' GMT ' . $this->_date->format('P') : $result;
}
  • Вопрос задан
  • 130 просмотров
Решения вопроса 1
@matveyvarg Автор вопроса
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы