We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Field-Types

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 http://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.

Jay George

Jay George 2 points

  • 2 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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 — http://forum.grabaperch.com/forum/02-23-2016-forum-reply-notifications

Drew McLellan

Drew McLellan 2638 points
Perch Support

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

<?php
class PerchTemplateFilter_timezone extends PerchTemplateFilter 
{
    public function filterBeforeProcessing($value, $valueIsMarkup = false)
    {
        /* Convert the time to UTC */
        $converted_event_time = new DateTime($value, new DateTimeZone('Europe/London'));
        $converted_event_time->format('YmdHis') . "\n";

        $converted_event_time->setTimezone(new DateTimeZone('UTC'));
        $converted_event_time->format('YmdHis') . "\n";

        $value = $converted_event_time->format('YmdHis') . "\n"); <-- This line causes a blank page

        return ($value);
    }
}
?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

define('PERCH_TEMPLATE_FILTERS', true);

Step 2: Build the filter

The following goes in perch/addons/templates/filters.php

<?php
class PerchTemplateFilter_bst_to_utc extends PerchTemplateFilter 
{
    public function filterBeforeProcessing($value, $valueIsMarkup = false)
    {
        /* Convert the time to UTC */
        $converted_event_time = new DateTime($value, new DateTimeZone('Europe/London'));
        $converted_event_time->format('YmdHis') . "\n";

        $converted_event_time->setTimezone(new DateTimeZone('UTC'));
        $converted_event_time->format('YmdHis') . "\n";

        $value = $converted_event_time->format('YmdHis') . "\n";

        return ($value);
    }
}
?>

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

<?php PerchSystem::register_template_filter('bst-to-utc', 'PerchTemplateFilter_bst_to_utc'); ?>

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:

<perch:content id="event_date_start" format="Y-m-d\THi" urlify="true" escape="true" filter="bst-to-utc"/>

Step 5: Call your HTML template using perch_content_custom

e.g. in index.php you might now have something like this:

PerchSystem::register_template_filter('bst-to-utc', 'PerchTemplateFilter_bst_to_utc');

perch_content_custom('Events', array(
    'template' => 'events/event_listing.html',
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

Instead of calling register_template_filter() in your page, you can do it in addons/templates/filters.php.

Thanks, I've amended the instructions.