1

Problem:
After a long time of research, I have stuck to find it's the solution
I applied the following code but it didn't work.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime');
 $date = $objDate->gmtDate();

Any help.
your help would be appreciated!

Version:
Magento 2.4.4

3 Answers 3

3

It's 2 general ways:

// current time or any time in UTC
$now = new \DateTime('now', new \DateTimeZone('UTC'));

// instances
$timezone = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
$locale   = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Framework\Locale\ResolverInterface::class)
    ->getLocale();

// example in custom format
$localeDate = $timezone->date(new \DateTime('now', new \DateTimeZone('UTC')), $locale);
echo $localeDate->format('m/d/y H:i:s');

// example with format from locale 
echo $timezone->formatDateTime(
    $now,
    \IntlDateFormatter::SHORT,
    \IntlDateFormatter::MEDIUM
);

Output:

06/06/22 12:12:03
6/6/22, 12:12:03 PM
3
  • Thanks for helping but these two method is not getting date from TimeZone in store's configuration as like time. getting date from my system clock. Jun 7 at 13:24
  • change you time in system's clock then check it getting date from your system. Jun 7 at 13:25
  • What is timezone set in configuration? Global/Store. What is scope? FE I have system time +3 and Magento -8 and getting correct date/time like all other in Magento. It's the logic which used in Magento Jun 7 at 14:08
2

Use below code.

use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class Yourclass extends AbstractClass
{
    protected $timezoneInterface;

    public function __construct(
        ...
     TimezoneInterface $timezoneInterface
     )
    {
        $this->timezoneInterface = $timezoneInterface;
         ...
    }

    public function getStoreDateTime()
    {
        $formatDate = $this->timezoneInterface->formatDate();        
        $dateTime = $this->timezoneInterface->date()->format('Y-m-d H:i:s');
        $date = $this->timezoneInterface->date()->format('Y-m-d');
        $time = $this->timezoneInterface->date()->format('H:i');
        
    }
}

Reference

2
  • time shows correctly but the date is showing by my system, not for the time zone location that are I set in store configuration. Jun 6 at 12:51
  • I want to fetch date from configure time zone area. Jun 6 at 12:53
1

Please try below code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$timezone = $objectManager->create('Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$today = $timezone->date()->format('m/d/y H:i:s');
$dateTimeAsTimeZone = $timezone->date(new \DateTime($dateTime))->format('m/d/y H:i:s');
2
  • time shows correctly but the date is showing by my system, not for the time zone location that are I set in store configuration. Jun 6 at 12:52
  • I want to fetch date from configure time zone area. Jun 6 at 12:53

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.