6

I have below script, trying to get all Orders. I have issue with Date. That i have solved using https://github.com/magento/magento2/issues/7560

//place this before any script you want to calculate time
date_default_timezone_set('Asia/Singapore');
echo date_default_timezone_get();

// MAGENTO START
include('app/bootstrap.php');

use Magento\Framework\App\Bootstrap;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\AccountManagement;

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

//$orders = $objectManager->create('Magento\Sales\Model\ResourceModel\Order\Collection');
$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection();

$magentoTimeZoneObject = $objectManager->create('Magento\Framework\Stdlib\DateTime\TimezoneInterface');

foreach ($orders as $order) {

    echo "<pre>";
    echo $order->getData('entity_id').' - '.$order->getData('created_at');

    echo $magentoTimeZoneObject->date(new \DateTime($order->getCreatedAt()))->format('Y-m-d H:i:s');
}

In Database i have 2 Orders for different dates. But when i fetch from Database it's showing wrong Created Date for Orders. It's not showing Database Dates.

  • try factory data like this: $objectManager->create('Magento\Sales\Model\OrderFactory')->create()->getCollection() – Mohammad Mujassam Nov 25 '16 at 5:10
  • HI @MohammadMujassam Nope Not Working Sorry – Ankit Shah Nov 25 '16 at 5:16
  • hope you checked the created date directly in sales_order table. just to verify. – Mohammad Mujassam Nov 25 '16 at 5:23
  • Yes on sales_order table is Correct. Don't know what i'm missing. Updating code for Timezone As well in question. It's not getting correct value from Database. It's connected to right database as well. Fetching wrong Order Created At. Don't know why? – Ankit Shah Nov 25 '16 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.