Answer the question
In order to leave comments, you need to log in
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;
}
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
https://stackoverflow.com/questions/9922430/dates-...
https://bugs.php.net/bug.php?id=45647
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question