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.

2
  • Are you testing it in localhost? Mar 27, 2017 at 13:25
  • yes i am using localhost now
    – Naveenbos
    Mar 28, 2017 at 5:23

4 Answers 4

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)

1
  • This script is not working
    – Naveenbos
    Mar 28, 2017 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');
1
  • i am not getting the value
    – Naveenbos
    Mar 28, 2017 at 5:24
0

Please try this, here you need to change 'Asia/Kolkata' as per you need zone.

date_default_timezone_set('Asia/Kolkata');
$date_value = $objectManager->create('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$today = $date_value->date()->format('m/d/y H:i:s');
$dateTimeAsTimeZone = $date_value->date(new \DateTime($today))->format('m/d/y H:i:s');

Hope it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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