Synopsis
I have a date field in my admin form but when I save this date & time, I then have an observer that runs on my model _beforeSave
which corrects the format for MySQL. Although in the database and in the fields after reload, the time is missing.
Observer
<?php
require_once Mage::getModuleDir('', 'Some_Banners') . '/Model/Slide.php';
class My_Banner_Model_slide extends Some_Banners_Model_Slide
{
public function _beforeSave()
{
parent::_beforeSave();
if (preg_match('/^\d{2}([\/]+)\d{2}\1\d{4}\s+\d{1,2}\:+\d{1,2}$/', $this->getEndAt())) {
$endDate = preg_replace('/^(\d{2}?)\/(\d{2}?)\/(\d{4}?)\s+(\d{1,2}?)\:+(\d{1,2}?)$/', '\\3-\\2-\\1 \\4:\\5:00', $this->getEndAt());
$this->setData('end_at', $endDate);
}
return $this;
}
}
Please note: when I execute the following debugging the data is correct as expected, something happens after this method before the save occurs.
if (preg_match('/^\d{2}([\/]+)\d{2}\1\d{4}\s+\d{1,2}\:+\d{1,2}$/', $this->getEndAt())) {
Zend_Debug::dump( $this->getEndAt() );
// Output: 13/05/2013 12:30
$endDate = preg_replace('/^(\d{2}?)\/(\d{2}?)\/(\d{4}?)\s+(\d{1,2}?)\:+(\d{1,2}?)$/', '\\3-\\2-\\1 \\4:\\5:00', $this->getEndAt());
Zend_Debug::dump( $endDate );
// Output: 2015-05-13 12:30:00
$this->setData('end_at', $endDate);
Zend_Debug::dump( $this->getEndAt() );
// Output: 2015-05-13 12:30:00
}