From 136630c290ad3e582da98121675713872cf1cd80 Mon Sep 17 00:00:00 2001 From: scossar Date: Tue, 8 May 2018 13:34:32 -0700 Subject: [PATCH] Add format_date function --- templates/template-functions.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/templates/template-functions.php b/templates/template-functions.php index a51ee0a..10b891a 100644 --- a/templates/template-functions.php +++ b/templates/template-functions.php @@ -114,4 +114,28 @@ class TemplateFunctions { return $parsed; } + + /** + * Format the Discourse created_at date based on the WordPress site's timezone. + * + * @param string $string The datetime string returned from Discourse. + * @param string $format The datetime format. + * + * @return false|string + */ + public static function format_date( $string, $format ) { + $tz = get_option( 'timezone_string' ); + $gmt_offset = get_option( 'gmt_offset' ); + $localtime = ''; + if ( $tz ) { + $datetime = date_create( $string, new \DateTimeZone( 'UTC' ) ); + $datetime->setTimezone( new \DateTimeZone( $tz ) ); + $localtime = $datetime->format( $format ); + } elseif ( $gmt_offset ) { + $timestamp = strtotime( $string ) + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS); + $localtime = gmdate( $format, $timestamp ); + } + + return $localtime; + } }