Append Site Timezone to End Date in DateRange in Drupal 8


Zebras. (I just wanted to use my "z" illumination.)

In general, it's not necessary to add a timezone to both start and end dates. But if you modify the date format, that's exactly what will happen.

So how do we add the site timezone just to the end of the DateTimeRange?

Here's how I did this:

function mytheme_preprocess_field(&$variables, $hook) {
  if ($variables['field_name'] == 'myfield') {
    if (isset($variables['items'][0]['content']['end_date']['#plain_text'])) {
      $dateTime = new DateTime();
      $dateTime->setTimeZone(new DateTimeZone(drupal_get_user_timezone()));
      $variables['items'][0]['content']['end_date']['#plain_text'] .= ' ' . $dateTime->format('T');

And it looks something like this: 8/2/17 5:15 PM - 8/2/17 10:15 PM EDT

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.