M
M
matveyvarg2017-11-10 09:28:04
PHP
matveyvarg, 2017-11-10 09:28:04

Why is the date formatting not happening correctly?

There is a method in the controller similar to smarty's date_format.
The input is a string like "0000-00-00 00:00:00", but both the method and date_format return the string "-0001-11-30", regardless of the current date.
Method Code:
(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;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
matveyvarg, 2017-11-10
@matveyvarg

https://stackoverflow.com/questions/9922430/dates-...
https://bugs.php.net/bug.php?id=45647

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question