Adobe Commerce 2.3 reached end of support in September 2022.

DateTime library

The DateTime library provides utilities to work with date and time formatting. Provided methods in this library are described below.

Usage

Method Description
formatDate Format the date to the Magento internal format
isEmptyDate Check whether provided date value is empty or null

Example

The following example shows how to format the current date and time into the Magento internal format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

use Magento\Framework\Stdlib\DateTime;

...
    /**
     * @var DateTime
     */
    private $dateTime;

    /**
     * SomeClass constructor.
     *
     * @param DateTime $dateTime
     */
    public function __construct(DateTime $dateTime)
    {
        $this->dateTime = $dateTime;
    }

    /**
     * display current date to internal format time zone
     *
     * @return string
     */
    public function getFormatDate(): string
    {
    return $this->dateTime->formatDate(time());
    }
...