Magento 2 Not Getting Current Time – Troubleshooting

magento2timetimezone

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.

Best Answer

    $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
Related Topic