Code Duplication    Length = 25-28 lines in 2 locations

includes/cli/class-wc-cli-command.php 1 location

@@ 210-234 (lines=25) @@
207
	 * @param  bool $convert_to_utc
208
	 * @return string RFC3339 datetime
209
	 */
210
	protected function format_datetime( $timestamp, $convert_to_utc = false ) {
211
		if ( $convert_to_utc ) {
212
			$timezone = new DateTimeZone( wc_timezone_string() );
213
		} else {
214
			$timezone = new DateTimeZone( 'UTC' );
215
		}
216
217
		try {
218
			if ( is_numeric( $timestamp ) ) {
219
				$date = new DateTime( "@{$timestamp}" );
220
			} else {
221
				$date = new DateTime( $timestamp, $timezone );
222
			}
223
224
			// convert to UTC by adjusting the time based on the offset of the site's timezone
225
			if ( $convert_to_utc ) {
226
				$date->modify( -1 * $date->getOffset() . ' seconds' );
227
			}
228
229
		} catch ( Exception $e ) {
230
			$date = new DateTime( '@0' );
231
		}
232
233
		return $date->format( 'Y-m-d\TH:i:s\Z' );
234
	}
235
236
	/**
237
	 * Get formatter object based on supplied arguments.

includes/api/class-wc-api-server.php 1 location

@@ 711-738 (lines=28) @@
708
	 * @param bool $convert_to_utc
709
	 * @return string RFC3339 datetime
710
	 */
711
	public function format_datetime( $timestamp, $convert_to_utc = false ) {
712
713
		if ( $convert_to_utc ) {
714
			$timezone = new DateTimeZone( wc_timezone_string() );
715
		} else {
716
			$timezone = new DateTimeZone( 'UTC' );
717
		}
718
719
		try {
720
721
			if ( is_numeric( $timestamp ) ) {
722
				$date = new DateTime( "@{$timestamp}" );
723
			} else {
724
				$date = new DateTime( $timestamp, $timezone );
725
			}
726
727
			// convert to UTC by adjusting the time based on the offset of the site's timezone
728
			if ( $convert_to_utc ) {
729
				$date->modify( -1 * $date->getOffset() . ' seconds' );
730
			}
731
732
		} catch ( Exception $e ) {
733
734
			$date = new DateTime( '@0' );
735
		}
736
737
		return $date->format( 'Y-m-d\TH:i:s\Z' );
738
	}
739
740
	/**
741
	 * Extract headers from a PHP-style $_SERVER array