magento2.2 database – How to Get Timestamp from DB in Magento 2

backenddatabasemagento2.2PHP

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

Best Answer

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');       
    }
Related Topic