Quebrando Cabeca
Quebrando Cabeca

Reputation: 325

difference with timezone in Custom Report Magento

I am creating a custom report in Magento. My server is located in the US and my shop is Brazilian. It turns out that buying schedules are saved in the American schedule, ie 3hours more than in Brazil. And that gives a difference in the report results.

Imagine the following situation: A customer makes a purchase on 30/06 at 10:00 PM (time of Brazil), in the database, this purchase comes as day 01/07 at 01:00 AM.

How do you pick the order creation time (GMT USA) and turn clockwise from Brazil?

Today, I'm using that.

      $to="";
$from="";
$show_order_statuses = 0;
$orserstatus = "";

$result_order = 0;

if(!empty($_REQUEST['from']) && !empty($_REQUEST['to'])){

    $orders_row = array();
    date_default_timezone_set('America/Sao_Paulo'); //define o timezone como Sãp Paulo  
    $filter_type = $_REQUEST['filter_type'];
    $from = $_REQUEST['from'];
    $to = $_REQUEST['to'];      
    $from_date = date('Y-m-d' . ' 00:00:00', strtotime($from));
    $to_date = date('Y-m-d' . ' 23:59:59', strtotime($to));


    $filter_model  = ($filter_type == 'shipping_date')
        ? 'sales/order_shipment_collection'
        : 'sales/order_collection';

    if($_REQUEST['show_order_statuses']>0){
        $orserstatus = $_REQUEST['order_statuses'];
        $_orderCollections = Mage::getResourceModel($filter_model);
            $_orderCollections->addAttributeToSelect('*');
            $_orderCollections->addFieldToFilter('created_at', array('from'=>$from_date, 'to'=>$to_date));
            if($filter_type == 'order_date'){
                $_orderCollections->addFieldToFilter('status', $orserstatus);
            }                
            $_orderCollections->setOrder('created_at', 'desc');
            $_orderCollections->load();
    }else{
        $_orderCollections = Mage::getResourceModel($filter_model)
            ->addAttributeToSelect('*')
            ->addFieldToFilter('created_at', array('from'=>$from_date, 'to'=>$to_date))
            ->setOrder('created_at', 'desc')
            ->load();
    }

Anyone who can help, thank you

Upvotes: 3

Views: 353

Answers (1)

Junaid Bhura
Junaid Bhura

Reputation: 327

You should always use Magento's core date function to account for timezones.

So instead of:

$from_date = date( 'Y-m-d' . ' 00:00:00', strtotime( $from ) );

Use:

$from_date = Mage::getModel( 'core/date' )->date( 'Y-m-d 00:00:00', strtotime( $from ) );

This will give you the date based on the timezone you have set in your store's configuration in the admin.

Upvotes: 1

Related Questions