Magento custom module date field saving date to on

2019-03-02 19:09发布

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

8条回答
ら.Afraid
2楼-- · 2019-03-02 19:19

This solved my problem, don't know whether it is the right way to do it or not but i was more concerned about solving it

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);
}
查看更多
小情绪 Triste *
3楼-- · 2019-03-02 19:25

I stumbled on this post, while browsing for similar question about dates in Magento. Unfortunately, these answers didn’t help me, but I came back to share a couple of things about dates which I hope will be useful for someone else.

First of all, Magento stores dates in DB in UTC timezone. This way it requires the same code and same logic to display info for your customers in UK, US or AU. Check carefully, it saves it not "one day before", but 8 hours before (25 Jan, 2012 00:00:00 => 2012-01-26 16:00:00).

Second important thing: Magento provides standard dynamically generated getters and setters for database fields. So if you have added your custom fields to Product or Customer you don’t need to define your own methods to read/write to database. Your class, something like: class MyOrg_MyModule_Model_Customer extends Mage_Customer_Model_Customer will have methods $this->getStart_date() and $this->setStart_date($datetime) even for your custom fields. You also can use method $this-setData('start_date', $datetime); and then $this->save();

This information will help you understand what TimeZone parameters are required by standard Magento functions:

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

For simplicity and speed, I suggest you to use timestamps for your date calculations and comparisons. And convert time in your timezone only in order to display it.

查看更多
淡お忘
4楼-- · 2019-03-02 19:28

if the date is one day before the selected one try

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

set 'useTimezone' to False in

/app/code/core/Mage/Core/Model/Locale.php

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

查看更多
Summer. ? 凉城
5楼-- · 2019-03-02 19:32

Be sure, when you save date you do some like this

$dt = $this->getResource()->formatDate(time());
$this->setData('created_at', $dt); // current model

Then for getting correct timezone date use some like this

$dt = $this->getData('created_at');
$time = $this->_getResource()->mktime($dt);
if ($time) {
    $dt = Mage::app()->getLocale()->date($time, null, null, true);
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-03-02 19:32

print_r() your data in controller and exit before saving it and look if you are still getting the right data before saving it. I never had this issue but it seems to be issue of your time zone.

查看更多
叼着烟拽天下
7楼-- · 2019-03-02 19:36
$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
}
查看更多
登录 后发表回答