2
0
Fork 0
mirror of https://github.com/discourse/wp-discourse.git synced 2025-10-03 08:59:21 +08:00

Add format_date function

This commit is contained in:
scossar 2018-05-08 13:34:32 -07:00
parent ab75e7ae2a
commit 136630c290

View file

@ -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;
}
}