Magento 1.4 Magento自定义模块日期字段将日期保存到所选日期前一天
我按照此链接上的步骤将日期字段添加到我的自定义模块: 一切都很顺利,除了当我选择日期并保存配置时,它会在所选日期前一天返回日期:( 例如: 当我选择2012年2月25日并保存时,它将保存并返回2012年2月24日 请注意,它是在一天前保存的:( 保存前,当我在管理员控制器中打印($model)时,我会得到以下信息:Magento 1.4 Magento自定义模块日期字段将日期保存到所选日期前一天,magento-1.4,magento-1.5,magento,Magento 1.4,Magento 1.5,Magento,我按照此链接上的步骤将日期字段添加到我的自定义模块: 一切都很顺利,除了当我选择日期并保存配置时,它会在所选日期前一天返回日期:( 例如: 当我选择2012年2月25日并保存时,它将保存并返回2012年2月24日 请注意,它是在一天前保存的:( 保存前,当我在管理员控制器中打印($model)时,我会得到以下信息: [start_date] => 2012-01-24 16:00:00 // i set it to 25 but its saving 24 [end_date] =>
[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
注意:
我回显了发布日期,我设置的意思是发布数据没有问题,
意思是客户端没有任何错误,所以问题出在何时转换为保存在数据库中!!!有什么帮助吗?
这是我的首字母代码我试过了:
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'));
}
然后我试了这个:
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'));
}
echo$format=Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::format_TYPE_SHORT)。“
;
如果($data['start_date']!=NULL)
{
echo$data['start_date'].“
”;///01/27/12我输入的正确发布日期
$date=Mage::app()->getLocale()->date($data['start\u date',$format);
echo$date;///2012年1月26日下午4:00:00,但现在我们回到一天
$time=$date->getTimestamp();
$model->setStartDate(Mage::getSingleton('core/date')->date(null,$time));
//$model->setStartDate($date->toString('YYYY-MM-dd HH:MM:ss');
}
如果($data['end_date']!=NULL)
{
echo$data['end_date'].
;
$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');
}
$格式回音:M/d/yy
原始发布日期:2012年1月27日
$date echo result:2012年1月26日下午4:00:00
print_r()
保存控制器中的数据并在保存之前退出,在保存之前查看您是否仍然获得正确的数据。我从未遇到过此问题,但它似乎是您所在时区的问题。请确保,在保存日期时,您会这样做
$dt = $this->getResource()->formatDate(time());
$this->setData('created_at', $dt); // current model
$dt = $this->getData('created_at');
$time = $this->_getResource()->mktime($dt);
if ($time) {
$dt = Mage::app()->getLocale()->date($time, null, null, true);
}
然后,为了获得正确的时区日期,请使用以下方法
$dt = $this->getResource()->formatDate(time());
$this->setData('created_at', $dt); // current model
$dt = $this->getData('created_at');
$time = $this->_getResource()->mktime($dt);
if ($time) {
$dt = Mage::app()->getLocale()->date($time, null, null, true);
}
发生了惊人的事情 我刚刚从管理员控制器中删除了保存日期字段的代码,一切都很好 它将日期自动保存在数据库中
这可能会帮助很多人并节省他们的时间不像我,我把大部分时间都花在了上面。这解决了我的问题,不知道这样做是否正确,但我更关心的是解决问题
if($data['start_date'] != NULL )
{
$start_time_array = explode("/", $data['start_date']);
$start_time = $start_time_array[2]."-".$start_time_array[0]."-".$start_time_array[1]." 00:00:00";
$model->setStartDate($start_time);
}
如果日期比所选日期早一天,请重试
Mage::app()->getLocale()->date($data['start_date'], Zend_Date::DATE_SHORT, null, false);
在中将“useTimezone”设置为False
/app/code/core/Mage/core/Model/Locale.php
日期($date=null,$part=null,$locale=null,$useTimezone=true)这是一个非常奇怪的问题。这就是在_filterDate函数中对我起作用的原因:
$value = date('Y-m-d', strtotime($value . ' + 1 days'));
我在浏览Magento中关于日期的类似问题时偶然发现了这篇文章。不幸的是,这些答案对我没有帮助,但我回来分享了一些关于日期的事情,我希望这些事情对其他人有用 首先,Magento将日期存储在UTC时区的DB中。这样,它需要相同的代码和逻辑来为您在英国、美国或非洲的客户显示信息。 仔细检查,它保存的不是“一天前”,而是8小时前(2012年1月25日00:00:00=>2012-01-26 16:00:00) 第二件重要的事情:Magento为数据库字段提供了标准的动态生成的getter和setter。因此,如果您已将自定义字段添加到产品或客户,则无需定义自己的方法来读取/写入数据库。您的类,例如:
class MyOrg\u MyModule\u Model\u Customer扩展了Mage\u Customer\u Model\C客户
将有方法$this->getStart\u date()
和$this->setStart\u date($datetime)
,即使对于自定义字段也是如此。您也可以使用方法$this setData('start\u date',$datetime);
然后$this->save();
此信息将帮助您了解标准Magento函数需要哪些时区参数:
$current_datetime_utc_in_timestamp_format = time();
$start_datetime_utc_in_text_format = $this->getStart_date();
$start_datetime_utc_in_timestamp_format = Varien_Date::toTimestamp($start_datetime_utc_in_text_format);
//check Mage/Core/Model/Date.php
// timestamp() adds TimeZone to datetime, see also gmtTimestamp() – it deducts TimeZone from datetime
$start_datetime_local_in_timestamp_format = Mage::getModel('core/date')->timestamp($start_datetime_utc_in_text_format) );
//Timestamp() also accepts parameter in timestamp format
$start_datetime_local_in_timestamp_format = Mage::getModel('core/date')->timestamp($current_datetime_utc_in_timestamp_format) );
$displayTime = true;
$start_datetime_local_in_text_format = Mage::helper('core')->formatDate($start_datetime_utc_in_text_format, 'medium', $displayTime);
//or
$start_datetime_local_in_text_format = Mage::getModel('core/date')->date("Y-m-d H:i:s", $start_datetime_utc_in_timestamp_format);
$start_datetime_utc_in_text_format = Mage::getModel('core/date')->gmtdate("Y-m-d H:i:s", $start_datetime_local_in_timestamp_format);
//similar to timestamp()/gmtTimestamp(), date() – adds TimeZone and gmtDate() – deducts TimeZone from time
$this->setStart_date( $start_datetime_utc_in_timestamp_format );
为了简单和快速,我建议您在日期计算和比较中使用时间戳。并且仅为了显示时间,才转换时区中的时间。您是否检查过表单提交的值是否正确?到达控制器操作时是否正确?如果不正确,则会出现客户端问题。如果提交正确:在您的
print\r
之前的echo date_default_timezone_get();
给出了什么内容?这是在本地完成的,但在实时站点上可以节省2011年11月31日的时间:(为什么会这样?我会尝试一下,并在这里发布回复,虽然这可能会回答问题,但最好解释答案的基本部分,可能还有OPs代码的问题所在。)。
$preSaleDate = $_product->getAvailabilityDate();//product attribute
if($preSaleDate) {
$format = 'Y-m-d H:i:s'; //current format
$formatted_date = DateTime::createFromFormat($format, $preSaleDate)->format('m/d/Y');
$dateReal = Mage::app()->getLocale()->date($formatted_date
,Zend_Date::DATE_SHORT, null, false);
$format = 'long'; // short, long, medium, full
$dueDate = Mage::helper('core')->formatDate($dateReal, $format, false);
echo $dueDate;// will get same day as original $preSaleDate
}