From 1a9c49db25f81e16965912c3ce4f784dd90a0af1 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 8 Mar 2017 16:50:38 +0000 Subject: [PATCH] set_date_prop helper to convert dates in setters --- includes/abstracts/abstract-wc-data.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php index 52b1139d05c..575bb80722e 100644 --- a/includes/abstracts/abstract-wc-data.php +++ b/includes/abstracts/abstract-wc-data.php @@ -588,6 +588,28 @@ abstract class WC_Data { return $value; } + /** + * Sets a date prop whilst handling formatting and datatime objects. + * + * @since 2.7.0 + * @param string $prop Name of prop to set. + * @param string|integer $value Value of the prop. + */ + protected function set_date_prop( $prop, $value ) { + try { + if ( empty( $value ) ) { + $datetime = null; + } elseif ( is_numeric( $value ) ) { + $datetime = new WC_DateTime( "@{$value}", new DateTimeZone( 'UTC' ) ); + $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + } else { + $datetime = new WC_DateTime( $value, new DateTimeZone( wc_timezone_string() ) ); + $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) ); + } + $this->set_prop( $prop, $datetime ); + } catch ( Exception $e ) {} + } + /** * When invalid data is found, throw an exception unless reading from the DB. *