0

my code for converting created_at in qatar time zone works in admin grid ,i need code for exporting createad at in qatar time, i have tried didn't get converted during export

public function __construct(
    TimezoneInterface $timezone,
    \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
    \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
    array $components = [],
    array $data = []
) {
    $this->timezone = $timezone;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        foreach ($dataSource['data']['items'] as &$item) {
            if (isset($item[$fieldName])) {
                // Convert time to Qatar timezone
                $gmtTime = $item[$fieldName];
                $qatarTimeZone = 'Asia/Qatar';
                $qatarTime = $this->timezone->date(new \DateTime($gmtTime))
                    ->setTimezone(new \DateTimeZone($qatarTimeZone))
                    ->format('Y-m-d H:i:s');
                $item[$fieldName] = $qatarTime;

            }
        }
    }
    return $dataSource;
}

}

1 Answer 1

0

Please try this

namespace Vendor\Module\Ui\Component\Listing\Column;

use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class QatarTime extends \Magento\Ui\Component\Listing\Columns\Column
{
    protected $timezone;

    public function __construct(
        TimezoneInterface $timezone,
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ) {
        $this->timezone = $timezone;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as &$item) {
                if (isset($item[$fieldName])) {
                    // Convert time to Qatar timezone
                    $gmtTime = $item[$fieldName];
                    $qatarTimeZone = 'Asia/Qatar';
                    $qatarTime = $this->timezone->date(new \DateTime($gmtTime))
                        ->setTimezone(new \DateTimeZone($qatarTimeZone))
                        ->format('Y-m-d H:i:s');
                    $item[$fieldName] = $qatarTime;
                }
            }
        }
        return $dataSource;
    }

    public function prepare()
    {
        parent::prepare();
        if ($this->getContext()->getRequestParam('namespace') == 'sales_order_grid') {
            $config = $this->getData('config');
            $config['label'] = __('Created At (Qatar Time)');
            $this->setData('config', $config);
        }
    }
}
<column name="created_at" class="Vendor\Module\Ui\Component\Listing\Column\QatarTime">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">dateRange</item>
            <item name="dataType" xsi:type="string">date</item>
            <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
            <item name="label" xsi:type="string" translate="true">Created At (Qatar Time)</item>
            <item name="sortOrder" xsi:type="number">30</item>
        </item>
    </argument>
</column>

Your Answer

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

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