在Magento 1.x中,您可以通过以下方式获取商店日期时间
Mage::getModel('core/date')->gmtDate();
Magento 2.x中的等效功能是什么?
在M2中,您需要在类构造函数中注入的实例\Magento\Framework\Stdlib\DateTime\DateTime并使用该实例。
像这样:
protected $date;
public function __construct(
....
\Magento\Framework\Stdlib\DateTime\DateTime $date,
....
) {
....
$this->date = $date;
....
}
然后,您可以使用以下代码:
$date = $this->date->gmtDate();
gmtDate上面显示的方法接受2个可选参数。第一个$format默认为Y-m-d H:i:s。您可以仅使用所需的参数gmtDate('H:i:s')或任何其他时间格式来调用该方法
