Magento自定义模块日期字段保存日期为所选日期之前的一天

问题描述 投票:1回答:8

我按照此链接上的步骤向我的自定义模块添加日期字段:

http://magentomechanic.blogspot.com/2010/01/to-add-custom-date-field-in-custom.html

一切都很顺利,除了当我选择日期并保存配置时,它会在选定的一天之前返回我的日期:(

例如 :

当我选择2012年2月25日并保存时,它将保存并返回2012年2月24日。

注意它在前一天保存:(

我在保存前在admin控制器中的print_r($ model)时得到这个:

[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'));
                }

$ format echoes:M / d / yy原始发布日期:01/27/12 $ date echo结果:2012年1月26日下午4:00:00

magento-1.4 magento-1.5 magento
8个回答
3
投票

我偶然发现了这篇文章,同时浏览了关于Magento日期的类似问题。不幸的是,这些答案对我没有帮助,但我回过头来分享一些关于日期的事情,我希望这对其他人有用。

首先,Magento以UTC时区的数据库存储日期。这样,它需要相同的代码和相同的逻辑来显示您在英国,美国或澳大利亚的客户的信息。仔细检查,它保存不是“前一天”,而是8小时前(2012年1月25日00:00:00 => 2012-01-26 16:00:00)。

第二个重要的事情:Magento为数据库字段提供标准的动态生成的getter和setter。因此,如果您已将自定义字段添加到Product或Customer,则无需定义自己的方法来读取/写入数据库。你的班级,如:class MyOrg_MyModule_Model_Customer extends Mage_Customer_Model_Customer即使你的自定义字段也会有方法$this->getStart_date()$this->setStart_date($datetime)。你也可以使用方法$this-setData('start_date', $datetime);然后$this->save();

此信息将帮助您了解标准Magento功能所需的TimeZone参数:

$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 );

为了简单和快速,我建议您使用时间戳进行日期计算和比较。并在时区中转换时间只是为了显示它。


2
投票

发生了很棒的事情!

我刚从管理员控制器中删除了保存日期字段的代码,所有这些都很好!

它会自动将日期保存在数据库中。

这可能会帮助很多人,并节省他们不像我的时间,我大部分时间花在它上面。


2
投票

这是一个非常奇怪的问题。这对我在_filterDate函数中有用:

$value = date('Y-m-d', strtotime($value . ' + 1 days'));

1
投票

如果日期是所选择的日期前一天尝试

Mage::app()->getLocale()->date($data['start_date'], Zend_Date::DATE_SHORT, null, false);

将'useTimezone'设置为False in

/app/code/core/mage/core/model/locale.PHP

date($ date = null,$ part = null,$ locale = null,$ useTimezone = true)


0
投票

请确保,当您保存日期时,您会做一些这样的事情

$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);
}

0
投票

这解决了我的问题,不知道是否是正确的方法,但我更关心解决它

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);
}

0
投票

print_r()您的数据在控制器中并在保存之前退出,并在保存之前查看您是否仍在获取正确的数据。我从来没有遇到这个问题,但它似乎是你的时区问题。


0
投票
$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
}
© www.soinside.com 2019 - 2024. All rights reserved.