0

I have tried different options but i am not getting the current time, time shows the gmt time, but actual time setting is asia/kolkatta, any one done this, please update the answer.

protected $datetime;

public function __construct(Context $context,
        ObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\DateTime\DateTime $datetime
    ) {
        $this->objectManager = $objectManager;
        $this->storeManager  = $storeManager;
        $this->datetime      = $datetime;
        parent::__construct($context);
    }
$current_time   = $this->datetime->gmtDate();// this will return only gmt time

Not the time i have set in the admin configuration locale timezone.

  • Are you testing it in localhost? – Sheshgiri Anvekar Mar 27 '17 at 13:25
  • yes i am using localhost now – Naveenbos Mar 28 '17 at 5:23
3
    $objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\Timezone');
    $currentTime = $objDate->formatDatetime(date("Y-m-d H:i:s"));
    $currentTimestamp = strtotime($objDate->formatDatetime(date("Y-m-d H:i:s")));//timestamp
0

Instead of below line:

\Magento\Framework\Stdlib\DateTime\DateTime $datetime

**Try to use this : **

 \Magento\Framework\Stdlib\DateTime\TimezoneInterface::formatDate(); 

(by injecting dependency to construct)

  • This script is not working – Naveenbos Mar 28 '17 at 5:23
0

You can try with format date as below:

public function __construct(Context $context,
        ObjectManagerInterface $objectManager,
        StoreManagerInterface $storeManager,
        \Magento\Framework\Stdlib\DateTime\DateTime \TimezoneInterface $datetime
    ) {

        $this->objectManager = $objectManager;
        $this->storeManager  = $storeManager;
        $this->datetime      = $datetime;
        parent::__construct($context);
    }

$current_time   = $this->datetime->date()->format('Y-m-d');
  • i am not getting the value – Naveenbos Mar 28 '17 at 5:24

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.