From dab7cf7dff441e9d58f8ed9145bebb738025756d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Mon, 15 Apr 2019 19:28:19 +0200 Subject: [PATCH] Respect manual offsets in default before/after params (https://github.com/woocommerce/woocommerce-admin/pull/2042) --- .../class-wc-admin-reports-interval.php | 18 ++++++++++++++---- .../class-wc-admin-reports-data-store.php | 12 ++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php index a182f15527f..ff7bc7c1a52 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-reports-interval.php @@ -44,8 +44,13 @@ class WC_Admin_Reports_Interval { * @return DateTime */ public static function default_before() { - $datetime = new DateTime(); - $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + $datetime = new WC_DateTime(); + // Set local timezone or offset. + if ( get_option( 'timezone_string' ) ) { + $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + } else { + $datetime->set_utc_offset( wc_timezone_offset() ); + } return $datetime; } @@ -58,9 +63,14 @@ class WC_Admin_Reports_Interval { $now = time(); $week_back = $now - WEEK_IN_SECONDS; - $datetime = new DateTime(); + $datetime = new WC_DateTime(); $datetime->setTimestamp( $week_back ); - $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + // Set local timezone or offset. + if ( get_option( 'timezone_string' ) ) { + $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + } else { + $datetime->set_utc_offset( wc_timezone_offset() ); + } return $datetime; } diff --git a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php index 71dbe50ce09..c559998ca0d 100644 --- a/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php +++ b/plugins/woocommerce-admin/includes/data-stores/class-wc-admin-reports-data-store.php @@ -572,13 +572,21 @@ class WC_Admin_Reports_Data_Store { ); if ( isset( $query_args['before'] ) && '' !== $query_args['before'] ) { - $datetime_str = $query_args['before']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); + if ( is_a( $query_args['before'], 'WC_DateTime' ) ) { + $datetime_str = $query_args['before']->date( WC_Admin_Reports_Interval::$sql_datetime_format ); + } else { + $datetime_str = $query_args['before']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); + } $sql_query['where_time_clause'] .= " AND {$table_name}.date_created <= '$datetime_str'"; } if ( isset( $query_args['after'] ) && '' !== $query_args['after'] ) { - $datetime_str = $query_args['after']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); + if ( is_a( $query_args['after'], 'WC_DateTime' ) ) { + $datetime_str = $query_args['after']->date( WC_Admin_Reports_Interval::$sql_datetime_format ); + } else { + $datetime_str = $query_args['after']->format( WC_Admin_Reports_Interval::$sql_datetime_format ); + } $sql_query['where_time_clause'] .= " AND {$table_name}.date_created >= '$datetime_str'"; }