Last active
January 26, 2018 00:09
-
-
Save mgussekloo/8ec5fa53c1505d032991 to your computer and use it in GitHub Desktop.
Timberdate-filter for Twig, to use in combination with Wordpress & Timber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add to something like functions.php | |
// https://www.skyverge.com/blog/down-the-rabbit-hole-wordpress-and-timezones/ | |
function wp_get_timezone_string() { | |
// if site timezone string exists, return it | |
if ( $timezone = get_option( 'timezone_string' ) ) | |
return $timezone; | |
// get UTC offset, if it isn't set then return UTC | |
if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) ) | |
return 'UTC'; | |
// adjust UTC offset from hours to seconds | |
$utc_offset *= 3600; | |
// attempt to guess the timezone string from the UTC offset | |
if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) { | |
return $timezone; | |
} | |
// last try, guess timezone string manually | |
$is_dst = date( 'I' ); | |
foreach ( timezone_abbreviations_list() as $abbr ) { | |
foreach ( $abbr as $city ) { | |
if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset ) | |
return $city['timezone_id']; | |
} | |
} | |
// fallback to UTC | |
return 'UTC'; | |
} | |
add_filter('get_twig', function($twig) { | |
$twig->addFilter('timberdate', new Twig_Filter_Function(function($timestamp, $format) { | |
$dt = new DateTime(); | |
$year = (int)date('Y', $timestamp); | |
if ($year < 1900 || $year > 3000) { | |
$timestamp = $timestamp / 1000; | |
} | |
$dt->setTimestamp($timestamp); | |
$dt->setTimezone(new DateTimeZone( | |
wp_get_timezone_string() | |
)); | |
return $dt->format($format); | |
})); | |
return $twig; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment