Skip to content

Instantly share code, notes, and snippets.

@dawoodman71
Last active November 3, 2021 14:39
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dawoodman71/5250759 to your computer and use it in GitHub Desktop.
Save dawoodman71/5250759 to your computer and use it in GitHub Desktop.
PHP Solution for adding a Google Calendar, injecting styles and changing options dynamically.
.dp-cur, .chip dt, .te, .te-t, .te-rev-s, .rb-n, .rb-i, .agenda, .event-title{
white-space:normal !important;
}
#calendarTitle,
.tab-name,
img[title="Print my calendar (shows preview)"],
.footer img,
.details .links a:last-child,
.event-links a:last-child
{display:none;}
.mv-event-container
{border-color:#999;}
.navBack,
.navForward
{ background-image: url(images/combined_v22.png); }
.agenda-more
{ color:#0D75BB; }
.agenda .date-label
{ padding:5px 0px; }
.dp-popup
{background:#fff;}
.view-cap, .view-container-border,
.mv-daynames-table,
.ui-rtsr-selected,
.wk-weektop,
.wk-dummyth,
.date-picker-arrow-on,
.dp-weekend-selected
{ background-color:#dfdfdf; }
.ui-rtsr-unselected,
.mv-daynames-table,
.wk-daynames,
.wk-dayname,
.dp-prev,
.dp-cur,
.dp-next
{ color:#666; }
.ui-rtsr-unselected
{ background-color:transparent;}
.dp-weekday-selected,
.agenda .date-label
{background-color:#efefef;}
.dp-today-selected
{background-color:#fad163;}
.mv-daynames-table,
.wk-daynames
{height:30px; font-size:14px;}
.mv-event-container
{ top:30px;}
<!-- by default the iframe shows the month view -->
<iframe id="googleCalendar" style="border: 0;"
src="google_calendar.php"
height="600" width="100%"
frameborder="0" scrolling="no"></iframe>
<!-- add ?v=[VIEW] to the path to force a view -->
<!-- views include month, week, agenda -->
<iframe id="googleCalendar?v=agenda" style="border: 0;"
src="google_calendar.php"
height="300" width="400"
frameborder="0" scrolling="no"></iframe>
<?php
// determine path, css filename and view mode
$calendarpath="https://www.google.com/calendar/embed?src=[YOUR GOOGLE CALENDAR ID]&ctz=America/New_York";
$newcss="google_calendar.css";
$defaultview=($_GET["v"]) ? $_GET["v"] : "month";
// import the contents of the Google Calendar page into a string
$contents = file_get_contents($calendarpath);
// add secure Google address to root relative links
$contents = str_replace('<link type="text/css" rel="stylesheet" href="', '<link type="text/css" rel="stylesheet" href="https://www.google.com/calendar/', $contents );
$contents = str_replace('<script type="text/javascript" src="', '<script type="text/javascript" src="https://www.google.com/calendar/' , $contents );
// inject css file reference
$contents = str_replace('<script>function _onload()', '<link rel="stylesheet" type="text/css" href="'.$newcss.'" /><script>function _onload()', $contents );
// update settings found in javascript _onload() function
$contents = str_replace('"view":"month"', '"view":"'.$defaultview.'"', $contents);
$contents = str_replace('"showCalendarMenu":true', '"showCalendarMenu":false', $contents);
if($defaultview == "month") $contents = str_replace('"showDateMarker":true', '"showDateMarker":false', $contents);
if($defaultview != "month") $contents = str_replace('"showTabs":true', '"showTabs":false', $contents);
echo $contents;
?>
@brightwillies
Copy link

` $newcss = 'g_calender.css'; $f = file_get_contents('https://calendar.google.com/calendar/embed?src=[calender_id]&ctz=Asia/Taipei'); $f = str_replace('//calendar.google.com','https://calendar.google.com',$f); $f = str_replace('/calendar/_','https://calendar.google.com/calendar/_',$f); $f = str_replace('<script>function _onload()', '<script>function _onload()', $f );

echo $f; `

this works perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment