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:
parent
ab75e7ae2a
commit
136630c290
1 changed files with 24 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue