. * * @since 1.1.0 * * @param bool $timestamp Optional. True to return a timestamp. False for a date string. * @param string $format Optional. The date format. * @return mixed The current date string or timestamp. False on failure. */ function wc_od_get_local_date( $timestamp = true, $format = 'Y-m-d' ) { $date = current_time( $format ); return ( $timestamp ? strtotime( $date ) : $date ); } /** * Gets the date format for the specified context. * * Added 'admin' context in version 1.2.0. * * The format can be translated for each language. It uses the ISO 8601 as the default date format. * It is recommended to use this method only for display purposes. To make date operations is better to use the standard ISO 8601. * * @since 1.1.0 * * @param string $context Optional. The context [php, js, admin]. * @return string The date format. */ function wc_od_get_date_format( $context = 'php' ) { $use_wp_format = _x( 'yes', "Use the WordPress date format for this language? Set to 'no' to use a custom format", 'woocommerce-order-delivery' ); $date_format = get_option( 'date_format' ); // Use the translated date format. if ( 'yes' !== $use_wp_format ) { $date_format = _x( 'Y-m-d', 'Custom PHP date format for this language', 'woocommerce-order-delivery' ); } if ( 'js' === $context ) { // Convert the date format from PHP to JS. Keep this order to avoid the double conversion of some characters. $format_conversion = array( 'd' => 'dd', 'j' => 'd', 'l' => 'DD', 'F' => 'MM', 'm' => 'mm', 'n' => 'm', 'y' => 'yy', 'Y' => 'yyyy', ); $date_format = str_replace( array_keys( $format_conversion ), array_values( $format_conversion ), $date_format ); } elseif ( 'admin' === $context ) { // Use the same format as the 'date' column. $format = __( 'M j, Y', 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch /** This filter is documented in woocommerce/includes/admin/list-tables/class-wc-admin-list-table-orders.php */ $date_format = apply_filters( 'woocommerce_admin_order_date_format', $format ); } /** * Filter the date format. * * @since 1.2.0 * * @param string $date_format The date format. * @param string $context The context [php, js, admin]. */ return apply_filters( 'wc_od_get_date_format', $date_format, $context ); } /** * Converts a timestamp to a WC_DateTime object. * * @since 2.3.0 * * @param int $timestamp The timestamp value. * @return WC_DateTime|false */ function wc_od_timestamp_to_datetime( $timestamp ) { try { $datetime = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) ); } catch ( Exception $e ) { return false; } // 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; } /** * Converts a date string to a WC_DateTime. * * @since 2.3.0 * * @param string $date_string Date string. * @param int $from_timestamp Optional. Timestamp to convert from. Default null. * @return WC_DateTime */ function wc_od_string_to_datetime( $date_string, $from_timestamp = null ) { if ( ! $from_timestamp ) { return wc_string_to_datetime( $date_string ); } $timestamp = wc_string_to_timestamp( $date_string, $from_timestamp ); return wc_od_timestamp_to_datetime( $timestamp ); } SOUTRAMALL - SOUTRAMARKET