2

I have created admin grid and ui form. If I add a item, created and updated timings are automatically saved in DB. I had fetched the Timings of created and modified. But it shows the date accurately.But failed to fetch the timing accurately (It shows six hours difference from the created or updated).Actual created timing is Nov 22, 2017 11:16:41 AM But it shows 2017-11-22 05:46:41.Please provide me a solution.

DataBase enter image description here I have got output like below. enter image description here

AdditionalInfo.php

<?php

namespace xxX\HomeSlider\Block\Adminhtml\Edit\Tab\View;

use Magento\Framework\Exception\NoSuchEntityException;
use XXx\HomeSlider\Model\Post;

class AdditionalInfo extends \Magento\Backend\Block\Template
{

    /**
     * Interval in minutes that shows how long customer will be marked 'Online'
     * since his last activity. Used only if it's impossible to get such setting
     * from configuration.
     */
    const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;

    protected $dateTime;
    protected $_coreRegistry;
    protected $authSession;

    public function __construct(
    \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Stdlib\DateTime\DateTime $datetime, \Magento\Backend\Model\Auth\Session $authSession, array $data = []
    )
    {
        $this->_coreRegistry = $registry;
        $this->authSession = $authSession;
        $this->datetime = $datetime;
        parent::__construct($context, $data);
    }

    public function getCurrentUser()
    {
        return $this->authSession->getUser()->getUsername();
    }

    public function getCreatedAt()
    {

        $postModel = $this->_coreRegistry->registry('homepageslider_post')->getCreatedAt();

        return $postModel;
    }

    public function getUpdatedAt()
    {

        $postModel = $this->_coreRegistry->registry('homepageslider_post')->getUpdatedAt();

        return $postModel;
    }

    public function getUpdatedUser()
    {
        return $this->authSession->getUser()->getUsername();
    }

}

additional_info.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
// @codingStandardsIgnoreFile

/** @var \OX\HomeSlider\Block\Adminhtml\Edit\Tab\View\AdditionalInfo $block */
$adminName = $block->getCurrentUser();
$createdAt = $block->getCreatedAt();
$updatedAt = $block->getUpdatedAt();
$updatedBy = $block->getUpdatedUser();
?>
<div class="fieldset-wrapper customer-information">
    <div class="fieldset-wrapper-title">
        <span class="title"><?= $block->escapeHtml(__('Additional Information')) ?></span>
    </div>
    <table class="admin__table-secondary">
        <tbody>
            <?= $block->getChildHtml() ?>
            <tr>
                <th><?= $block->escapeHtml(__('Created By:')) ?></th>
                <td><?= $block->escapeHtml($adminName) ?></td>
            </tr>
            <tr>
                <th><?= $block->escapeHtml(__('Created At:')) ?></th>
                <td><?= $block->escapeHtml($createdAt) ?></td>
            </tr>
             <tr>
                <th><?= $block->escapeHtml(__('Updated At:')) ?></th>
                <td><?= $block->escapeHtml($updatedAt) ?></td>
            </tr>
            <tr>
                <th><?= $block->escapeHtml(__('Updated By:')) ?></th>
                <td><?= $block->escapeHtml($updatedBy) ?></td>
            </tr>
        </tbody>
    </table>


</div>
<?php

2 Answers 2

3

Goto store->configration->General->Locale Options

change Timezone to India Standard Time (Asia/Kolkata)

Use this to get current date and time:-

    public function __construct(Context $context,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
     ...)
    {
        ...
        $this->timezone = $timezone;
        ....
    }
    public function execute()
    {
        $date = $this->timezone->date();
        $date = $date->format('d/m/y H:i:s A');       
    }
0
2

use $this->datetime->date() to get the correct timestamp.

For example

public function getCreatedAt()
{

    $postModel = $this->_coreRegistry->registry('homepageslider_post')->getCreatedAt();

    return $this->datetime->date($postModel);
}

public function getUpdatedAt()
{

    $postModel = $this->_coreRegistry->registry('homepageslider_post')->getUpdatedAt();

    return $this->datetime->date($postModel);
}
3
  • Yes, it should work. Commented Nov 22, 2017 at 6:58
  • But it is not working for me. again it shows the time difference . When the item saved the timing in database is 12:21:09 PM but the output shows 6:51:09
    – Jaisa
    Commented Nov 22, 2017 at 7:00
  • It works for only date not the timing
    – Jaisa
    Commented Nov 22, 2017 at 8:41

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.