Add an Event to Google Calendar in PHP
Google Calendar is a free, cloud-based scheduling product developed and maintained by Google, and it is used as a tool for event management and reminders. Learn how to add an event to Google Calendar in PHP using the Google Calendar API for smooth event management.

Google Calendar is widely used by individuals, businesses and organizations to add events/reminders for important meetings, tasks and appointments. Web developers also use it to simplify the automated processes of event management in web applications. In this post, we are going to learn how to add an event to Google Calendar in PHP by utilizing the Google Calendar API. The following files need to be created for this post
- index.php: The HTML page with an event form.
- google-calendar-events.php: The server-side script code to add an event to Google Calendar.
- style.css: The CSS stylesheet for the event HTML page.
How to Add an Event to Google Calendar using Calendar API
Adding an event to Google Calendar requires some basic prerequisites, such as enabling the Google Calendar API, a configured Google Cloud project with client credentials and a Google Calendar to add events to. We will create a user interface with a form to submit event data to a server-side script, and then the server-side script will make requests to the Google Calendar API to add an event to a calendar. Steps to add an event to Google Calendar are as follows:
Step 1: Install Google Client API Library
We need a client library to make requests to the Google Calendar API from our web application. So we first need to install the Google Client API library via Composer. This library can also be downloaded as a zip archive.
composer require google/apiclient:^2.15.0
Step 2: Create a Cloud Console Project
Next, we need to create a Cloud Console project to access and communicate with Google Services. To create a project, go to Google Cloud Console and do the following:
- Select an existing project or click on "New Project".
- Enter a project name and click Create.

Step 3: Create and Download Service Account Credentials
In order to securely communicate with Google APIs, we need client credentials to make authenticated API requests to Google.
- Click "IAM and admin" from the left navigation menu.
- In the submenu, click on "Service accounts".
- Click on "+ Create service account".
- Enter a service account name and description if needed.
- Click on "Done".
- Click the "Keys" tab on the Create Service Account page.
- Click "Add key" and select "JSON" for Key type.
- Click "Create" to download a JSON key file. Save it in the project directory.

Step 4: Create a Google Calendar
We also need a Google Calendar to which we will add events from our web application. We create a calendar in Google and share it with the Google service account we created. Go to the Google Calendar page and:
- Click the Settings cog icon.
- Click "Create new calendar" under "Add calendar" in the left side menu on the settings page.
- Enter a calendar name and description.
- Click the "Create Calendar" button.
- After the calendar has been created, click on the three vertical dots icon next to the calendar name in the calendars list.
- Find the section "Shared with" and click on "Add people and groups".
- Enter the email address of the service account and click "Send".

Step 5: Create an HTML Event Form
Now that we have our service account credentials and calendar created, it's time to do the coding part. We will create a user interface with an HTML form containing the fields for event details. This form will be submitted to a server-side PHP script to create an event in Google Calendar with the submitted event details. The following are the steps to create an HTML event form:
- Start a session at the top to display success messages.
- Create a form with the required fields for events.
- We are adding fields for "calendar id", "event summary", "event location", "event description", "start time" and "end time".
- For a complete list of events, visit the official calendar API page of Google.
index.php
<?php
if (!session_id()) {
session_start();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Event to Google Calendar in PHP - Demo</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport"/>
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<section class="section py-4">
<div class="container">
<?php if (!empty($_SESSION['success'])) { ?>
<div class="alert alert-green">
Event added successfully.
</div>
<?php unset($_SESSION['success']); ?>
<?php } ?>
<div class="mb-4">
<form action="google-calendar-events.php" method="POST" class="form-google-calendar-event">
<div class="mb-4">
<label class="inline-block mb-1">Calendar ID <span class="text-red">*</span></label>
<input type="text" name="calendar_id" class="form-control" placeholder="Calendar ID..."/>
</div>
<div class="mb-4">
<label class="inline-block mb-1">Summary <span class="text-red">*</span></label>
<input type="text" name="summary" class="form-control" placeholder="Event summary..."/>
</div>
<div class="mb-4">
<label class="inline-block mb-1">Location <span class="text-red">*</span></label>
<input type="text" name="location" class="form-control" placeholder="Event location..."/>
</div>
<div class="mb-4">
<label class="inline-block mb-1">Description <span class="text-red">*</span></label>
<input type="text" name="description" class="form-control" placeholder="Event description..."/>
</div>
<div class="mb-4">
<label class="inline-block mb-1">Start <span class="text-red">*</span></label>
<input type="datetime-local" name="start" class="form-control"
placeholder="Event start date and time..."/>
</div>
<div class="mb-4">
<label class="inline-block mb-1">End <span class="text-red">*</span></label>
<input type="datetime-local" name="end" class="form-control"
placeholder="Event end date and time..."/>
</div>
<div class="mb-4">
<button type="submit" class="btn btn-blue">Add Event</button>
</div>
</form>
</div>
</div>
</section>
</body>
</html>
Step 6: Create an Event in Google Calendar using Calendar API (PHP)
When the event form is submitted to the server-side PHP script, we can easily add an event to Google Calendar using the Google API client library with the credentials we created. Steps to create a Google Calendar event using the Google client library:
- Include the vendor autoload to use the Google API client library.
- Start a session to store success messages.
- Sanitize form post fields. You can add more filters as per your requirements.
- Initialize the Google client class instance.
- Set the config credentials of the service account.
- Assign the Google Calendar scope of the Google service calendar to the client class instance.
- Initialize the Google service calendar and assign it to the Google
$calendar_servicevariable. - Use the form submitted values as parameters to create a Google Calendar event instance.
- Add an event to Google Calendar using the
$calendar_serviceinstance. - Set success status in the session and redirect the user back to the event form page.
google-calendar-events.php
<?php
include_once __DIR__ . '/vendor/autoload.php';
if (!session_id()) {
session_start();
}
$post = filter_input_array(INPUT_POST);
// Initialize Google client instance
$client = new Google_Client();
// Set service account config
$client->setAuthConfig('PATH_TO_SERVICE_ACCOUNT_CREDENTIALS');
// Set Google Calendar scope
$client->setScopes(Google_Service_Calendar::CALENDAR);
// Initialize Google Calendar service
$calendar_service = new Google_Service_Calendar($client);
date_default_timezone_set('America/Los_Angeles');
$start_date_time = date('Y-m-d\TH:i:s', strtotime($post['start']));
$end_date_time = date('Y-m-d\TH:i:s', strtotime($post['end']));
$timezone_offset = date('P');
// Create Google Calendar event
$event = new Google_Service_Calendar_Event([
'summary' => $post['summary'],
'location' => $post['location'],
'description' => $post['description'],
'start' => ['dateTime' => sprintf('%s%s', $start_date_time, $timezone_offset)],
'end' => ['dateTime' => sprintf('%s%s', $end_date_time, $timezone_offset)],
]);
$calendar_service->events->insert($post['calendar_id'], $event);
$_SESSION['success'] = true;
// Redirect user
header('Location: ' . filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_URL));
exit;
Step 7: Add CSS Styles
Add CSS styles for the entire user interface that contains the event form.
style.css
* {
box-sizing: border-box;
}
html,body {
margin: 0;
padding: 0;
}
body {
background-color: #f6f6f6;
font-family: "Segoe UI", "Roboto", "Helvetica", sans-serif;
font-size: 15px;
font-weight: normal;
font-style: normal;
}
.container {
max-width: 1024px;
margin: 0 auto;
padding-left: 15px;
padding-right: 15px;
}
.inline-block {
display: inline-block;
}
.py-4 {
padding-top: 1rem;
padding-bottom: 1rem;
}
.my-1, .mb-1 {
margin-bottom: .25rem;
}
.my-4, .mb-4 {
margin-bottom: 1rem;
}
.alert {
position: relative;
padding: .75rem 1.25rem;
margin-bottom: 1rem;
transition:opacity 0.5s;
}
.alert-green {
background-color: #319764;
border: 1px solid #248A57;
color: #ffffff;
}
.form-control {
display: block;
width: 100%;
padding: .375rem .75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #1c2528;
background-color: #ffffff;
background-clip: padding-box;
border: 1px solid #d1d2d3;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.btn {
display: inline-block;
padding: .375rem .75rem;
cursor: pointer;
font-size: inherit;
}
.btn-blue {
background-color: #0369a1;
border: 1px solid #0369a1;
color: #ffffff;
}
.btn-blue:hover {
background-color: #005D95;
}Update Google Calendar Event in PHP
We can also update a Google Calendar event in PHP using the client API library. We only need $calendar_id and $event_id and the event parameters that we want to update for a given Google Calendar event. Code snippet to update a Google Calendar event is:
$calendar_service->events->update($post['calendar_id'], $event_id, [
'summary' => $post['summary'],
'location' => $post['location'],
'description' => $post['description'],
'start' => ['dateTime' => sprintf('%s%s', $start_date_time, $timezone_offset)],
'end' => ['dateTime' => sprintf('%s%s', $end_date_time, $timezone_offset)],
]);
Delete an Event From Google Calendar in PHP
Just like updating an event in Google Calendar, we can also delete a specific event from Google Calendar by passing the $calendar_id and $event_id to the calendar service. The code snippet to delete an event from Google Calendar is like this:
$calendar_service->events->delete($post['calendar_id'], $event_id);
We demonstrated how to add, update and delete Google Calendar events in PHP using the Google Calendar API and a service account. We can automate scheduling and manage events programmatically with no manual effort by integrating Google Calendar into PHP applications. The code snippets in this post can also be used to add a birthday to Google Calendar by just passing the parameter eventType = 'birthday' to the event instance. Check out additional Google Calendar API features Google offers, such as recurring events, attendees, reminders and event types to further improve the functionality.