Skip to content

Timezone for twig date function not working as expected #2104

@columbian-chris

Description

@columbian-chris

I'm using Pacific timezone in daylight savings time in my example, which is UTC-7 hours.

Out of the box, both {{ date('now')|date('F j, Y @ g:i a') }} (the date function) and {{ 'now'|date('F j, Y @ g:i a') }} (the date filter) return UTC time with no timezone applied to them.

When I stick date_default_timezone_set('America/Los_Angeles'); at the top of my functions.php theme file, the date filter displays the correct current time (UTC-7), but the date function is displaying UTC-14, almost like the equivalent timezone offset is being applied twice.

After reading the Twig docs on the date() function, I thought adding the following in my functions.php file might work, but it isn't affecting my date variables in twig:

function add_to_twig( $twig ) {
	$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('America/Los_Angeles');
	return $twig;
}

I couldn't find anything in the Timber docs about timezone usage or manipulation. I have tried this on two different servers producing the same results.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions