wc_rest_prepare_date_response

Parses and formats a date for ISO8601/RFC3339.

Description

(string|null) wc_rest_prepare_date_response( (string|null|WC_DateTime) $date, (bool) $utc = true ); 

Required WP 4.4 or later. See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/

Returns (string|null)

ISO8601/RFC3339 formatted datetime.

Parameters (2)

0. $date (string|null|WC_DateTime)
The date.
1. $utc — Optional. (bool) => true
Send false to get local/offset time.

Usage

  1. if ( !function_exists( 'wc_rest_prepare_date_response' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-rest-functions.php'; 
  3.  
  4. // The date. 
  5. $date = null; 
  6.  
  7. // Send false to get local/offset time. 
  8. $utc = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_rest_prepare_date_response($date, $utc); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-rest-functions.php  
  1. function wc_rest_prepare_date_response( $date, $utc = true ) { 
  2. if ( is_numeric( $date ) ) { 
  3. $date = new WC_DateTime( "@$date", new DateTimeZone( 'UTC' ) ); 
  4. $date->setTimezone( new DateTimeZone( wc_timezone_string() ) ); 
  5. } elseif ( is_string( $date ) ) { 
  6. $date = new WC_DateTime( $date, new DateTimeZone( 'UTC' ) ); 
  7. $date->setTimezone( new DateTimeZone( wc_timezone_string() ) ); 
  8.  
  9. if ( ! is_a( $date, 'WC_DateTime' ) ) { 
  10. return null; 
  11.  
  12. // Get timestamp before changing timezone to UTC. 
  13. return gmdate( 'Y-m-d\TH:i:s', $utc ? $date->getTimestamp() : $date->getOffsetTimestamp() );