i followed steps on this link to add a date field to my custom module :
http://magentomechanic.blogspot.com/2010/01/to-add-custom-date-field-in-custom.html
Everything went fine, except for the that when i select a date and save configurations, it returns me date one day before the selected one :(
For Example :
When i select 25 Feb, 2012 and save , it will save and return 24 Feb, 2012.
Notice it saved one day before :(
i get this when i print_r($model) in admin controller before save:
[start_date] => 2012-01-24 16:00:00 // i set it to 25 but its saving 24
[end_date] => 2012-01-26 16:00:00 // i set it to 27 but .....
[status] => 1 [content] => asdasdadsd
[created_time] => 2012-01-25 07:27:11 // it gives current date and it is O'rite
[update_time] => 2012-01-25 07:27:11 ) //it gives current date and it is O'rite
NOTE:
i echo the posted date and it was right what i set to mean there is no problem with the post data, mean client side is clear for any bug, so where the problem lies is when it is converted to save in database !!! Any help ???
Here is my initiall code i tried :
if($data['start_date'] != NULL )
{
$date = Mage::app()->getLocale()->date($data['start_date'], Zend_Date::DATE_SHORT);
$model->setStartDate($date->toString('YYYY-MM-dd HH:mm:ss'));
}
if($data['end_date'] != NULL)
{
$date1 = Mage::app()->getLocale()->date($data['end_date'], Zend_Date::DATE_SHORT);
$model->setEndDate($date1->toString('YYYY-MM-dd HH:mm:ss'));
}
then i tried this one :
echo $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT).'<br/>';
if($data['start_date'] != NULL )
{
echo $data['start_date']."<br/>"; // 01/27/12 correct date posted which i entered
$date = Mage::app()->getLocale()->date($data['start_date'], $format);
echo $date; /// Jan 26, 2012 4:00:00 PM but here we get back to one day
$time = $date->getTimestamp();
$model->setStartDate(Mage::getSingleton('core/date')->date(null, $time));
//$model->setStartDate($date->toString('YYYY-MM-dd HH:mm:ss'));
}
if($data['end_date'] != NULL)
{
echo $data['end_date'].'<br/>';
$date1 = Mage::app()->getLocale()->date($data['end_date'], $format);
$time = $date1->getTimestamp();
$model->setEndDate(Mage::getSingleton('core/date')->date(null, $time));
//$model->setEndDate($date1->toString('YYYY-MM-dd HH:mm:ss'));
}
$format echoes : M/d/yy original posted date : 01/27/12 $date echo result :Jan 26, 2012 4:00:00 PM
Well amazing stuff happened !
i just removed the code from admin controller that was saving the dates fields and All good to go !!!
It saved the date automatically in the DB.
This may help many and save them time unlike me, i spent most of the time on it.
This is a really odd issue. This is what worked for me in a _filterDate function: