Skip to content

Instantly share code, notes, and snippets.

@hkitago
Last active March 3, 2019 23:41
Show Gist options
  • Save hkitago/f227b6781333f6ed9b54fb03a81ac2bc to your computer and use it in GitHub Desktop.
Save hkitago/f227b6781333f6ed9b54fb03a81ac2bc to your computer and use it in GitHub Desktop.
for EyeTV 250 users in Japanese
<?php
/* Replace the URL / file path with the .ics url */
$file = "https://www.dropbox.com/s/xi4htg3cpo9eraz/FIFAWC2018_JA-JP.ics?dl=1";
/* Getting events from isc file */
$obj = new ics();
$icsEvents = $obj->getIcsEventsAsArray( $file );
/* Here we are getting the timezone to get the event dates according to gio location */
$timeZone = trim ( $icsEvents [1] ['X-WR-TIMEZONE'] );
unset( $icsEvents [1] );
usort($icsEvents, function($a, $b) {
$startDtA = new DateTime ($a['DTSTART']);
$startDtB = new DateTime ($b['DTSTART']);
return $startDtA->getTimestamp() - $startDtB->getTimestamp();
});
$html = '<table><tr><td> ID </td><td> Event </td><td> Start at </td><td> End at </td><td> Station </td></tr>';
$i = 0;
foreach( $icsEvents as $icsEvent){
/* Getting start date and time */
$start = isset( $icsEvent ['DTSTART;VALUE=DATE'] ) ? $icsEvent ['DTSTART;VALUE=DATE'] : $icsEvent ['DTSTART'];
/* Converting to datetime and apply the timezone to get proper date time */
$startDt = new DateTime ( $start );
$startDt->setTimeZone ( new DateTimezone ( $timeZone ) );
$startDate = $startDt->format ( 'Y-m-d H:i' );
/* Getting end date with time */
$end = isset( $icsEvent ['DTEND;VALUE=DATE'] ) ? $icsEvent ['DTEND;VALUE=DATE'] : $icsEvent ['DTEND'];
$endDt = new DateTime ( $end );
if($i > 47) {
$endDt->add(new DateInterval('PT1H'));
}
$endDt->setTimeZone ( new DateTimezone ( $timeZone ) );
$endDate = $endDt->format ( 'Y-m-d H:i' );
/* Getting the name of event */
$eventName = trim($icsEvent['SUMMARY']);
$stationName = substr(strrchr(str_replace('\n',"\n",$icsEvent['DESCRIPTION']), 10), 1 );
$stationName = strpos($stationName,'/') ? stristr($stationName,'/',true) : $stationName;
$html .= '<tr><td>'.$i.'</td><td>'.$eventName.'</td><td>'.$startDate.'</td><td>'.$endDate.'</td><td>'.$stationName.'</td></tr>';
$iepg = 'Content-type: application/x-tv-program-info; charset=shift_jis'."\r\n";
$iepg .= 'version: 1'."\r\n";
$iepg .= 'station: コンポジットビデオ'."\r\n";
$iepg .= 'year: '.$startDt->format ( 'Y' )."\r\n";
$iepg .= 'month: '.$startDt->format ( 'm' )."\r\n";
$iepg .= 'date: '.$startDt->format ( 'd' )."\r\n";
$iepg .= 'start: '.$startDt->format ( 'H:i' )."\r\n";
$iepg .= 'end: '.$endDt->format ( 'H:i' )."\r\n";
$iepg .= 'program-title: FIFAWC2018 '.$eventName."\r\n";
$iepg .= 'genre: 1'."\r\n";
$iepg .= 'subgenre: 2'."\r\n";
$file = 'iepg-'.$i.'.tvpi';
$data = mb_convert_encoding(trim($iepg), "SJIS", "auto");
file_put_contents('iepg/'.$file, $data, LOCK_EX);
$i++;
}
echo $html.'</table>';
/*****************************************************************************
*
* Class to Import calendar events from ICAL .ics file in PHP | apptha https://www.apptha.com/blog/import-google-calendar-events-in-php/
*
****************************************************************************/
class ics {
/* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
function getIcsEventsAsArray($file) {
$icalString = file_get_contents ( $file );
$icsDates = array ();
/* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
$icsData = explode ( "BEGIN:", $icalString );
/* Iterating the icsData value to make all the start end dates as sub array */
foreach ( $icsData as $key => $value ) {
$icsDatesMeta [$key] = explode ( "\n", str_replace(["\r\n", "\r"], "\n", $value) );
}
/* Itearting the Ics Meta Value */
foreach ( $icsDatesMeta as $key => $value ) {
foreach ( $value as $subKey => $subValue ) {
/* to get ics events in proper order */
$icsDates = $this->getICSDates ( $key, $subKey, $subValue, $icsDates );
}
}
return $icsDates;
}
/* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
function getICSDates($key, $subKey, $subValue, $icsDates) {
if ($key != 0 && $subKey == 0) {
$icsDates [$key] ["BEGIN"] = $subValue;
} else {
$subValueArr = explode ( ":", $subValue, 2 );
if (isset ( $subValueArr [1] )) {
$icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
}
}
return $icsDates;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment