Forum
Output date as UTC in HTML Template
I'm outputting a date field in an HTML template, to be used as part of an Outlook calendar link. Outlook needs the time format in UTC.
I'm pretty sure I can do this with Perch using $PERCH_TZ somehow but I just can't connect the dots.
To put it in context, how can I make the below output as UTC?
<perch:content id="event_date_start" format="His" urlify="true" escape="true"/>
P.s. This is almost exactly the same question as https://forum.grabaperch.com/forum/02-20-2015-output-perch-timezone-or-outputting-dates-as-utc-in-template but I thought it might fly under the radar if I post to a closed thread. It seems Robert hinted at a solution but as I say, I can't connect the dots.
You can output it any way you like using the format attribute and either strtime formatting or date.
You already have format in there, just change it.
https://docs.grabaperch.com/templates/field-types/date/
Thanks Rachel.
I'm still not sure how to convert the inputted time from BST to UTC though, despite trying various different strftime and date formats.
The user will input the date in British Summer Time, but it then needs to be output as UTC. I think I can maybe pass this into PHP, convert it, then pass the variable back into the template (working on that now). Either that's the only way, or I don't understand how to convert it using the reference guide.
If you need to convert between timezones, then that's best done in PHP. You can use a template filter for this.
Thanks,
Oh wow, template filters are new to me. I'll have a go and post a solution or any further questions.
By the way, I'm not getting email notifications about forum replies any more. I've checked my preferences and spam. Looks like something like this has happened before — https://forum.grabaperch.com/forum/02-23-2016-forum-reply-notifications
I'll give the queue a kick.
Great, got that notification. I'm almost there on the template filters solution (I think). Looks like they're new in Perch 3 as well. Just in time!
Please could you give me a pointer on why this is returning a blank page? My working PHP knowledge is pretty limited. I'm trying to perform a time-zone conversion on the value, then push it back
You have an unpaired closing bracket. You should be getting an error pointing that out in your error log.
OOooooh such a rookie mistake. Amazing, this is working now. For anyone visiting this thread in future who wants to convert time zones here is the solution, using London -> UTC as an example:
This example is built upon this initial example: https://grabaperch.com/blog/archive/template-filters-in-perch-3
Step 1:
Enable filters, adding the following to your config:
Step 2: Build the filter
The following goes in
perch/addons/templates/filters.php
Step 3: Register the filter in your template
This can go in your php template (e.g. index.php), but it's neater to put it in perch/addons/templates/filters.php, after your filter rule
Step 4: Add your filter to the HTML template you're calling (note the "filter" property)
Here is an example. The format needs to be in this value for the timezone filter to work:
Step 5: Call your HTML template using perch_content_custom
e.g. in index.php you might now have something like this:
Instead of calling
register_template_filter()
in your page, you can do it inaddons/templates/filters.php
.Thanks, I've amended the instructions.