Magento 2: How to get current date, date time with format in .phtml file?
سؤال
I already referred Magento 2 Get Current Store Date Time
But how to use directly in .phtml
file? Can able to get Date, DateTime & Also able to set it's format?
المحلول
You can use this in phtml
file :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
$date = $objDate->gmtDate();
But using directly object manager in phtml
file is not a good practice.
نصائح أخرى
protected $_date;
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date)
{
$this->_date = $date;
}
then use like this
$this->_date->date()->format('Y-m-d H:i:s');
Hello i am using same \Magento\Framework\Stdlib\DateTime\TimezoneInterface in my code but it displays different when i print that in file and is different in database. why?? in database i have type "updated_at timestamp"
what if I want to give specific date here
$this->_date->date(<my-comment-date>)->format('Y-m-d H:i:s');
لا تنتمي إلى magento.stackexchange