LoginSignup
3
7

More than 5 years have passed since last update.

iOS(iPhone) にPHPからカレンダーに予定を追加してみた。

Last updated at Posted at 2014-11-19

大体
http://stackoverflow.com/questions/6735500/create-ical-calendar-event-with-php
のソースいじっただけですが。

後タグに登録してあるZendframeworkを単にMVCとしてビューとロジック分けてるだけなのでそこで上のURLとソースが変わってきているっていう事情もあります。
なのでZendじゃないと動かないソースってわけでもないです。
殆どは普通のPHPです。
まずはロジック(コントローラー側)

TestController.php
<?php
class Test_IndexController extends Zend_Controller_Action {
    public function testAction() {
        $sDateTime = '2014-11-19 10:00:00';
        $eDateTime = '2014-11-19 13:00:00';
        $summary = 'summary';
        $address = 'address';
        $description = 'description';

        $this->addcalender($this, $sDateTime, $eDateTime, $summary, $address, $description);
    }

    private function addcalender($parent, $sDateTime, $eDateTime, $summary, $address, $description) {
        $filename = 'calender.ics';
        header('Content-type: text/calendar; charset=utf-8');
        header('Content-Disposition: attachment; filename=' . $filename);

        $start = new DateTime($sDateTime);
        $startdate = $this->dateToCal($start->format('U'));
        $end = new DateTime($eDateTime);
        $enddate = $this->dateToCal($end->format('U'));
        $date = new DateTime();
        $now = $date->format('H:i:s');

        $parent->view->uid = md5(uniqid(mt_rand(), true)) . "@yourdate.test";
        $parent->view->datetime = $now;
        $parent->view->datestart = $startdate;
        $parent->view->dateend = $enddate;
        $parent->view->summary = $this->escapeString($summary);
        $parent->view->address = $this->escapeString($address);
        $parent->view->description = $this->escapeString($description);
        $parent->view->url = $this->escapeString('url');
    }

    private function dateToCal($timestamp) {
        return gmdate('Ymd\THis\Z', $timestamp);
    }

    // Escapes a string of characters
    private function escapeString($string) {
        return preg_replace('/([\,;])/','\\\$1', $string);
    }
}

なるほど、iCalもicsファイル使ってるんですねぇ。

ビューコードです。
smarty:test.tpl
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
UID:{$uid}
DTSTAMP:{$datetime}
DTSTART:{$datestart}
DTEND:{$dateend}
SUMMARY:{$summary}
LOCATION:{$address}
DESCRIPTION:{$description}
URL;VALUE=URI:{$url}
END:VEVENT
END:VCALENDAR

Androidはテスト中ですが、カレンダーWebアプリケーションなら色々使い道ありそうですね。

3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7