Home › Forums › Calendar Products › Event Aggregator › Function to automatically change any Greenwich time events to local time
- This topic has 5 replies, 2 voices, and was last updated 7 years, 9 months ago by
Citizen LocalMotive.
-
AuthorPosts
-
June 19, 2018 at 8:56 am #1556211
Citizen LocalMotive
ParticipantHi,
I would love a utility to automatically screen events for Greenwich time and convert them to the default local time of my WordPress site. I am having chronic problems with imported events coming in with the wrong time.Here is a code snippet from a previous post. Can this be modified for the purpose?
Thank you.
<?php
2 class Imported_Events_Timezone_Modifier {
3 protected $source;
4 protected $target_timezone;
5
6 public function __construct( $source, $target_timezone ) {
7 $this->source = $source;
8 $this->target_timezone = $target_timezone;
9 }
10
11 public function __invoke( $event, $record ) {
12 # Only run if the event is being imported from the specified source
13 if ( $this->source === null || $this->source === $record->meta[‘source’] ) {
14 add_action(
15 ‘tribe_events_update_meta’,
16 [ $this, ‘post_creation’ ]
17 );
18 }
19
20 return $event;
21 }
22
23 public function post_creation( $event_id ) {
24 # Run once only
25 remove_action(
26 ‘tribe_events_update_meta’,
27 [ $this, ‘post_creation’ ]
28 );
29
30 $new_start_date = $this->convert( get_post_meta( $event_id, ‘_EventStartDateUTC’, true ) );
31 $new_end_date = $this->convert( get_post_meta( $event_id, ‘_EventEndDateUTC’, true ) );
32
33 if ( $new_start_date && $new_end_date ) {
34 update_post_meta( $event_id, ‘_EventStartDate’, $new_start_date );
35 update_post_meta( $event_id, ‘_EventEndDate’, $new_end_date );
36 update_post_meta( $event_id, ‘_EventTimezone’, $this->target_timezone );
37 }
38 }
39
40 protected function convert( $datetime ) {
41 try {
42 $datetime = new DateTime( $datetime );
43 $datetime->setTimezone( new DateTimeZone( ‘UTC’ ) );
44 $datetime->setTimezone( new DateTimeZone( $this->target_timezone ) );
45 return $datetime->format( Tribe__Date_Utils::DBDATETIMEFORMAT );
46 }
47 catch ( Exception $e ) {
48 return false;
49 }
50 }
51 }
52
53 function modify_imported_event_timezones( $source_feed, $target_timezone ) {
54 $modifier = new Imported_Events_Timezone_Modifier( $source_feed, $target_timezone );
55 add_filter( ‘tribe_aggregator_before_insert_event’, $modifier, 10, 2 );
56 }
57
58 # 1) Set the first param to null to operate on all import sources
59 # 2) Update the second param to the desired timezone (it must be
60 # a valid timezone supported and understood by your PHP runtime)
61 modify_imported_event_timezones(
62 ‘https://some.source/ical-feed.ics’,
63 ‘America/Vancouver’
64 );June 19, 2018 at 9:13 am #1556240Citizen LocalMotive
ParticipantCan Modern Tribe just leave the times as the user entered them and *not* convert to Greenwich time.
If a person enters 6 pm to 8 pm, just post it aa 6 pm to 8 pm please and ignore the time zone.
Stop converting, please. When you convert it is always wrong.
Thank you.
June 19, 2018 at 9:26 am #1556268Citizen LocalMotive
ParticipantHere is exactly what is happening that is wrong. If the event organizer leaves the timezone blank, then when our calendar imports the event, it converts the time to UTC0 (stop that). So 6 pm to 8 pm becomes 3 am to 5 am UTC0. Then when it displays, the calendar tries to convert it back to the local time but is making a daylight savings time error. So it ends up being posted as 5 pm to 7 pm.
Stop all the converting and just leave it as entered.
June 19, 2018 at 9:28 am #1556272Cliff
MemberHi. Thanks for your detailed request. I found this snippet has been updated since the version you pasted in your reply:
https://gist.github.com/andrasguseo/c895e4a7755055c18148b5bbd482c1dd
Please reference https://theeventscalendar.com/knowledgebase/implementing-custom-code-snippets/ for how to implement custom code snippets.
Please let me know how this goes for you.
June 19, 2018 at 9:43 am #1556336Citizen LocalMotive
ParticipantThank you. Follow up clarification question: If I copy this into the
functions.php file, will this make it so *all* events with UTC time zone
get corrected automatically whenever an import is run? Or do I have to
run something? Does it take care of the daylight savings time error
that is chronically occurring when converting from UTC to local time?June 20, 2018 at 4:16 pm #1557728Cliff
MemberI’m not the author of this code snippet, and I haven’t personally tried it, but it looks like the main update is that it added support for All Day events.
Other than that, I’d guess it doesn’t do much different than what it did previously. The snippet’s title is meant to do what it says:
Modifies the timezone and the time of imported events, if the source feed timezone is UTC
July 12, 2018 at 9:35 am #1573742Support Droid
KeymasterHey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.
Thanks so much!
The Events Calendar Support Team -
AuthorPosts
- The topic ‘Function to automatically change any Greenwich time events to local time’ is closed to new replies.
