Timezone Difference in Custom Report Magento

I am creating my own report in Magento. My server is in the US and my store is Brazilian. It turns out that buy charts are saved in the US chart, i.e. 3 hours ahead of Brazil. And that makes a difference in the report results.

Imagine the following situation: a customer makes a purchase on 06/30 at 10:00 (Brazilian time), in the database this purchase arrives as 07/01 at 01:00.

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

I am using this today.

      $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, thanks

+3


source to share


1 answer


You should always use Magento core date function to keep track of time zones.

So instead of:

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

      



Using:

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

      

This will give you a date based on the timezone that you set in your store configuration in admin.

+1


source







All Articles