WC_Product_CSV_Importer::parse_datetime_field
Parse dates from a CSV. Dates can be Unix timestamps or in any format supported by strtotime().
Method of the class: WC_Product_CSV_Importer{}
No Hooks.
Returns
String|null.
Usage
$WC_Product_CSV_Importer = new WC_Product_CSV_Importer(); $WC_Product_CSV_Importer->parse_datetime_field( $value );
- $value(string) (required)
- Field value.
WC_Product_CSV_Importer::parse_datetime_field() WC Product CSV Importer::parse datetime field code WC 10.8.1
public function parse_datetime_field( $value ) {
try {
// If value is a Unix timestamp, convert it to a datetime string.
if ( is_numeric( $value ) ) {
$datetime = new DateTime( "@{$value}" );
// Return datetime string in ISO8601 format (eg. 2018-01-01T00:00:00Z) to preserve UTC timezone since Unix timestamps are always UTC.
return $datetime->format( 'Y-m-d\TH:i:s\Z' );
}
// Check whether the value is a valid date string.
if ( false !== strtotime( $value ) ) {
// If the value is a valid date string, return as is.
return $value;
}
} catch ( Exception $e ) {
// DateTime constructor throws an exception if the value is not a valid Unix timestamp.
return null;
}
// If value is not valid Unix timestamp or date string, return null.
return null;
}