WC_API_Server::format_datetime( int|string $timestamp, bool $convert_to_utc = false, bool $convert_to_gmt = false )

Format a unix timestamp or MySQL datetime into an RFC3339 datetime


Description Description


Parameters Parameters

$timestamp

(Required) unix timestamp or MySQL datetime

$convert_to_utc

(Optional)

Default value: false

$convert_to_gmt

(Optional) Use GMT timezone.

Default value: false


Top ↑

Return Return

(string) RFC3339 datetime


Top ↑

Source Source

File: includes/legacy/api/v2/class-wc-api-server.php

	public function format_datetime( $timestamp, $convert_to_utc = false, $convert_to_gmt = false ) {
		if ( $convert_to_gmt ) {
			if ( is_numeric( $timestamp ) ) {
				$timestamp = date( 'Y-m-d H:i:s', $timestamp );
			}

			$timestamp = get_gmt_from_date( $timestamp );
		}

		if ( $convert_to_utc ) {
			$timezone = new DateTimeZone( wc_timezone_string() );
		} else {
			$timezone = new DateTimeZone( 'UTC' );
		}

		try {

			if ( is_numeric( $timestamp ) ) {
				$date = new DateTime( "@{$timestamp}" );
			} else {
				$date = new DateTime( $timestamp, $timezone );
			}

			// convert to UTC by adjusting the time based on the offset of the site's timezone
			if ( $convert_to_utc ) {
				$date->modify( -1 * $date->getOffset() . ' seconds' );
			}
		} catch ( Exception $e ) {

			$date = new DateTime( '@0' );
		}

		return $date->format( 'Y-m-d\TH:i:s\Z' );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.1 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.