1

I want to load the current local time in a custom phtml file, that I want to load in my CMS Page.

I currently have the following code but that loads the GMC time, not my local time.

How can I change this?

CODE:

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
$date = $objDate->gmtDate();
$ndate = date('H:i', strtotime($date));
?>
<?php echo $ndate;?>
2
  • Is your location and server location one time zone? – Evgeniy Kapelko Dec 14 '19 at 23:00
  • @EvgeniyKapelko Yes it is! It seems to load the GMC time instead of my own time. – JGeer Dec 15 '19 at 10:44
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');
echo $dateTimeAsTimeZone = $date_value->date(new \DateTime($today))->format('H:i');

for one more

date_default_timezone_set('Asia/Kolkata'); 
$timefc = $objectManager->create('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
echo $timefc->formatDateTime($this->getCreatedAt());

for the only current time

date_default_timezone_set('Asia/Kolkata'); 
$objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
echo $date = $objDate->gmtDate('H:i');

Hope it works!

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.