如何创建向日历添加条目的链接?

我在这家夜总会工作,目前正在为他们制作一个网站,他们有很多事件,他们的网站围绕事件建立了很多,今天他们在 Facebook 上为每个事件都建立了一个事件,但是如果有一个“添加到我的日历”按钮,将它添加到你的 iCal 或谷歌日历(或者甚至 Outlook) ,那将是杀手锏。

我已经设法弄清楚如何制作一个日历提要,然后它将被添加为一个新的日历,唯一的好处是,人们可以“订阅事件”,但它是相当混乱的为每个事件有一个日历。所以我想知道如何实现一个功能,让他们添加到他们的主日历容易。像 mailto: link 一样思考,但如果可能的话,可以用于日历。

同样,在我谈论这个话题的时候,有没有人知道是否有可能将一个事件从 Facebook 自动导入到 WordPress,或者将一个事件从 WordPress 导出到 Facebook,虽然这会很棒,但是这并不重要。

200906 次浏览

UPDATE (free for personal use):
HTTPS IS NOW SUPPORTED

While my answer below detailing how-to for each service WILL work, IMO it's much easier now to go with a third-party like AddThisEvent [https://addthisevent.com]. It lets you customize lots of options as well as add to Facebook and more. Unfortunately, they've now made it a paid service for anything other than personal use and do enforce this.

I assume there are other third-party solutions like this one, but I can only speak to this one, and it has worked great for us so far.


For an "Add to my Google Calendar", they used to have a code generator form you could use, but have since taken it down. For more details on Google Calendar links, see squarecandy's answer below.

For Outlook, it's a BIT more complicated, but basically you need to create a .vcs file with the event's data, and just make a link to that file. Step-by-step instructions here.

For an iCal link, you could use a PHP class like this one, or follow this page's instructions on how to create an ics file (iCal file).

You can have the program create an .ics (iCal) version of the calendar and then you can import this .ics into whichever calendar program you'd like: Google, Outlook, etc.

I know this post is quite old, so I won't bother inputting any code. But please comment on this if you'd like me to provide an outline of how to do this.

The links in Dave's post are great. Just to put a few technical details about the google links into an answer here on SO:

Google Calendar Link

<a href="http://www.google.com/calendar/event?action=TEMPLATE&text=Example%20Event&dates=20131124T010000Z/20131124T020000Z&details=Event%20Details%20Here&location=123%20Main%20St%2C%20Example%2C%20NY">Add to gCal</a>

the parameters being:

  • action=TEMPLATE (required)
  • text (url encoded name of the event)
  • dates (ISO date format, startdate/enddate - must have both start and end time - the button generator will let you leave the endtime blank, but you must have one or it won't work.)
    • to use the user's timezone: 20131208T160000/20131208T180000
    • to use global time, convert to UTC, then use 20131208T160000Z/20131208T180000Z
    • all day events, you can use 20131208/20131209 - note that the button generator gets it wrong. You must use the following date as the end date for a one day all day event, or +1 day to whatever you want the end date to be.
  • details (url encoded event description/details)
  • location (url encoded location of the event - make sure it's an address google maps can read easily)

Update Feb 2018:

Here's a new link structure that seems to support the new google version of google calendar w/o requiring API interaction:

https://calendar.google.com/calendar/r/eventedit?text=My+Custom+Event&dates=20180512T230000Z/20180513T030000Z&details=For+details,+link+here:+https://example.com/tickets-43251101208&location=Garage+Boston+-+20+Linden+Street+-+Allston,+MA+02134

New base url: https://calendar.google.com/calendar/r/eventedit

New parameters:

  • text (name of the event)
  • dates (ISO date format, startdate/enddate - must have both start and end time)
    • an event w/ start/end times: 20131208T160000/20131208T180000
    • all day events, you can use 20131208/20131209 - end date must be +1 day to whatever you want the end date to be.
  • ctz (timezone such as America/New_York - leave blank to use the user's default timezone. Highly recommended to include this in almost all situations. For example, a reminder for a video conference: if three people in different timezones clicked this link and set a reminder for their "own" Tuesday at 10:00am, this would not work out well.)
  • details (url encoded event description/details)
  • location (url encoded location of the event - make sure it's an address google maps can read easily)
  • add (comma separated list of emails - adds guests to your new event)

Notes:

  • the old url structure above now redirects here
  • supports https
  • deals w/ timezones better
  • accepts + for space in addition to %20 (urlencode vs rawurlencode in php - both work)

To add to squarecandy's google calendar contribution, here the brand new

OUTLOOK CALENDAR format (Without a need to create .ics) !!

<a href="https://bay02.calendar.live.com/calendar/calendar.aspx?rru=addevent&dtstart=20151119T140000Z&dtend=20151119T160000Z&summary=Summary+of+the+event&location=Location+of+the+event&description=example+text.&allday=false&uid=">add to Outlook calendar</a>

test it

Best would be to url_encode the summary, location and description variable's values.

For the sake of knowledge,

YAHOO CALENDAR format

<a href="https://calendar.yahoo.com/?v=60&view=d&type=20&title=Summary+of+the+event&st=20151119T090000&et=20151119T110000&desc=example+text.%0A%0AThis+is+the+text+entered+in+the+event+description+field.&in_loc=Location+of+the+event&uid=">add to Yahoo calendar</a>

test it

Doing it without a third party holds a lot of advantages for example using it in emails.

Litmus has a great post detailing how to build a neat solution that works well across the major email and calendar clients, including catering for users who use clients from different vendors.

It is far from trivial, given the non-standardised nature of this space, but if you're willing to roll up your sleeves for a couple of days, you can get something that works well and doesn't come with a monthly price tag and limits on usage!

https://www.litmus.com/blog/how-to-create-an-add-to-calendar-link-for-your-emails/

You can generate a ics file which include the meeting information

the ics file look like this

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.6.4//EN
METHOD:REQUEST
BEGIN:VEVENT
UID:c7a913c7-3276-4828-8606-b841f2870475
DTSTAMP:20220519T070000Z
SUMMARY: Booking you for this date
DESCRIPTION:You having been invited to the event
DTSTART:20220519T070000Z
DURATION:PT1H
ORGANIZER;CN=Company Team:mailto:info@XXXXXX.com
ATTENDEE;ROLE=REQ-PARTICIPANT;CN=Not Amazing Attendee:mailto:*********@gmail.com
END:VEVENT
END:VCALENDAR

also the java code to generate ics file is here

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;


public class iCal {


private String version = "VERSION:1.0 \n";
private String prodid = "PRODID://Elara/lofy/tanare/delp/314sum2015// \n";
private String calBegin = "BEGIN:VCALENDAR \n";
private String calEnd = "END:VCALENDAR \n";
private String eventBegin = "BEGIN:VEVENT \n";
private String eventEnd = "END:VEVENT \n";


public void iCal() {
}


public static void main(String[] args) {


}


public void write(String name) {
StringBuilder builder = new StringBuilder();
builder.append(name);
builder.append(".ics");


String testExample = "UID:uid1@example.com\\nDTSTAMP:19970714T170000Z\\nORGANIZER;\n"
+ "    CN=John Doe:MAILTO:john.doe@example.com\\nDTSTART:19970714T170000Z\n"
+ "    \\nDTEND:19970715T035959Z\\nSUMMARY:Bastille Day Party\\n";


try {


File file = new File(builder.toString());


// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}


FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(calBegin);
bw.write(version);
bw.write(prodid);
bw.write(eventBegin);
bw.write(testExample);
bw.write(eventEnd);
bw.write(calEnd);


bw.close();


System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}