WC_Data::set_date_prop
Sets a date prop whilst handling formatting and datetime objects.
Method of the class: WC_Data{}
No Hooks.
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->set_date_prop( $prop, $value );
- $prop(string) (required)
- Name of prop to set.
- $value(string|int) (required)
- Value of the prop.
Changelog
| Since 3.0.0 | Introduced. |
WC_Data::set_date_prop() WC Data::set date prop code WC 10.3.6
protected function set_date_prop( $prop, $value ) {
try {
if ( empty( $value ) || '0000-00-00 00:00:00' === $value ) {
$this->set_prop( $prop, null );
return;
}
if ( is_a( $value, 'WC_DateTime' ) ) {
$datetime = $value;
} elseif ( is_numeric( $value ) ) {
// Timestamps are handled as UTC timestamps in all cases.
$datetime = new WC_DateTime( "@{$value}", new DateTimeZone( 'UTC' ) );
} elseif ( is_string( $value ) ) {
// Strings are defined in local WP timezone. Convert to UTC.
if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $value, $date_bits ) ) {
$offset = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
$timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
} else {
$timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $value ) ) ) );
}
$datetime = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) );
} else {
// If we get here, the value is not a valid date type.
$this->error( 'invalid_date', __( 'Invalid date provided.', 'woocommerce' ) );
}
// 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() );
}
$this->set_prop( $prop, $datetime );
} catch ( Exception $e ) {} // @codingStandardsIgnoreLine.
}