0

在 Magento 中,我有一个 phtml 迭代从 Mage_Core_Model_Abstract 继承的自定义模型的集合。该模型有一个名为“日期”的数据库字段。

当 phtml 向浏览器显示它正在迭代的每个项目的 getDate() 函数的输出时,大多数情况下它会显示正确的日期。对于不明确的日期(可以解释为 M/d/Y 或 d/M/Y 的日期,它做错了。我检查了 MySQL 表中的日期字段(datetime),并且所有日期都正确存储。PHP 中的一些东西是一旦从数据库中读取日期,就会错误地解释日期。

例如:

In MySQL            => HTML Output => status
2013-12-05 00:00:00 => 5/12/2013   => Wrong
2013-11-30 00:00:00 => 11/30/2013  => Correct
2013-11-06 00:00:00 => 6/11/2013   => Wrong
2013-10-11 00:00:00 => 11/10/2013  => Wrong
etc.

我已经完成了谷歌搜索并查看了StackOverflow,但我不确定如何解决这个问题。有任何想法吗?谢谢。

更新:我在集合上使用 printLogQuery(true) 来获取它正在执行的查询。它很简单: SELECT main_table.* FROM physicians_eventAS main_table;

为了提供有关日期如何从 DB 到 HTML 的更多背景信息: 在 phtml 中,我迭代了一个自定义集合:

<?php foreach ($this->getEventCollection() as $_event): ?>
    <?php echo $this->getLayout()->createBlock('physicians/event')
                    ->setEvent($_event)
                    ->setTemplate('physicians/event/row.phtml')
                    ->toHtml() ?>
<?php endforeach ?>

getEventCollection 看起来像这样:

public function getEventCollection() {
    if ($this->getSortBy() === null) {
        $this->setSortBy('date');
    }

    if ($this->getLimit() === null) {   
        $this->setLimit(10);
    }

    $collection = Mage::getModel('physicians/event')->getCollection();
    $collection->setOrder($this->getSortBy())
               ->setPageSize($this->getLimit());

    if ($this->getEventType() !== null) {
        $collection->addFieldToFilter('type', array('eq' => $this->getEventType()));
    }

    return $collection;
}

当所有内容都输出为 HTML(为集合中的每个项目呈现的 row.phtml)时,日期输出如下:

<?php echo $this->getEvent()->getFormattedDate('M/d/Y') ?>

getFormattedDate 函数如下所示:

public function getFormattedDate($format = null, $puredate=false) {
    $date = new Zend_Date($this->getDate());
    if ($format === null) {
        $format = 'MMMM d, Y';
    }
    return (!$puredate)?$date->toString($format):$date;
}

我相信这是迄今为止的全部内容。想法?再次感谢你!

更新:进一步调试。在 getFormattedDate 内部,我添加了输出语句来跟踪数据出错的位置:

echo "Collection Item Date: " . var_export($this->getDate()) . "<br/>\n";
$date = new Zend_Date($this->getDate());
echo "Zend Item Date: " . var_export($date,true) . "<br/>\n";

输出如下所示:

Collection Item Date: '2013-07-06 00:00:00'

Zend Item Date: Zend_Date::__set_state(array(
   '_locale' => 'en_US',
   '_fractional' => 0,
   '_precision' => 3,
   '_unixTimestamp' => '1370563200',
   '_timezone' => 'UTC',
   '_offset' => 0,
   '_syncronised' => 0,
   '_dst' => false,
))

这两项都是正确的 2013 年 7 月 6 日。我猜它稍后在代码中会变得混乱。痕迹还在继续……

我添加了这个:

echo "<pre>" . var_export($date->toString($format),true) . "</pre><br />\n";

其输出为:'6/7/2013',这是错误的。所以问题在于 $date->toString($format) 如何运作......

我现在通过这里看看有什么问题:http: //framework.zend.com/manual/1.12/en/zend.date.constants.html#zend.date.constants.selfdefinedformats

4

1 回答 1

2

我解决了这个问题!!!

问题在于:

$date = new Zend_Date($this->getDate());

它不喜欢将 DB 模型的日期传递给 Zend_Date 构造函数。Zend_Date 解释不正确。在阅读了 Zend_Date 上的文档后,我了解到它更喜欢 unix 时间戳。

我将 getFormatted Date 中的那一行更改为如下所示:

$date = new Zend_Date(strtotime($this->getDate()));

现在所有日期都正常工作!

于 2013-08-23T16:03:06.777 回答