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?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI 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.
Fatal error: Call to undefined method Magento\Framework\Stdlib\DateTime::gmtDate()
Using Magento 2
$block->formatDate($_posts->getCreatedAt());
got it. No worries Thanks :)
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');