1

I wanted to get the current date and time as created_at. I have tried all the ways I found on google but nothing works for me. It showing error while running my module.

   use Magento\Framework\View\Result\PageFactory; 
protected $_timezone;
    /**
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
        Context $context,
        \Magento\Framework\Stdlib\DateTime\DateTimeFactory $timezone,
        PageFactory $resultPageFactory
    ) {
        $this->_timezone = $timezone;
        $this->resultPageFactory = $resultPageFactory;
        return parent::__construct($context,$resultPageFactory);
    }
public function execute()
    {
        $date = $this->_timezone->gmtDate();
        echo $date; die;
}

But when i am using i am getting this error: Magento\Framework\ObjectManager\Factory\Compiled->create('Neo\Demo\Control...', array()) called at [vendor/magento/framework/ObjectManager/ObjectManager.php:56]

0

Use below code

use Magento\Framework\View\Result\PageFactory; 
protected $_timezone;
    /**
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
        Context $context,
        \Magento\Framework\Stdlib\DateTime\DateTime $timezone,
        PageFactory $resultPageFactory
    ) {
        $this->_timezone = $timezone;
        $this->resultPageFactory = $resultPageFactory;
        return parent::__construct($context,$resultPageFactory);
    }
public function execute()
    {
        $date = $this->_timezone->gmtDate();
        echo $date; die;
}

Reference

  • Thank you Qaisar but i am getting same error: (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: – neomai Aug 22 at 8:53
  • Delete generated folder – Qaisar Satti Aug 22 at 8:59
  • Do the recompliation – Amit Bera Aug 22 at 9:08
  • Thank you Qaisar now its working after deleting generated folder and cache refresh – neomai Aug 22 at 9:11
  • @neomai kindly accept the answer. so it will help future readers. – Qaisar Satti Aug 22 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.