How to merge two ".ics" files in to one?
<p>im getting ics files from multiple sources. what i want to do is merge those two files using PHP, but i have no idea how to merge those two files. ?</p>

<p>this is first sample file.</p>

<pre><code>BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//HomeAway.com, Inc.//EN
BEGIN:VEVENT
UID:e753a377-cc46-403f-a192-c25990400c0e
DTSTAMP:20161222T071905Z
DTSTART:20160728
DTEND:20160731
SUMMARY:Reserved - Janna Stotz
END:VEVENT
BEGIN:VEVENT
UID:989df323-1c44-4d95-ada7-e738dafa0590
DTSTAMP:20161222T071905Z
DTSTART:20170227
DTEND:20170304
SUMMARY:Reserved - Douglas Lawson
END:VEVENT
BEGIN:VEVENT
UID:3d997ba7-8aee-4836-a159-99d79eaf8f73
DTSTAMP:20161222T071905Z
DTSTART:20170213
DTEND:20170220
SUMMARY:Reserved - Stephen Harrison
END:VEVENT
BEGIN:VEVENT
UID:98a31bee-d25e-43a3-a5a4-9d443ea43b22
DTSTAMP:20161222T071905Z
DTSTART:20161222
DTEND:20161227
SUMMARY:Reserved - John Albert
END:VEVENT
END:VCALENDAR
</code></pre>

<p>this is second file below:</p>

<pre><code>    BEGIN:VCALENDAR
    PRODID;X-RICAL-TZSOURCE=TZINFO:-//Airbnb Inc//Hosting Calendar 0.8.8//EN
    CALSCALE:GREGORIAN
    VERSION:2.0
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160720
    DTSTART;VALUE=DATE:20160716
    UID:cn31z4tbyhod--eusv3c9eu9i0@airbnb.com
    DESCRIPTION:CHECKIN: 07/16/2016\nCHECKOUT: 07/20/2016\nNIGHTS: 4\nPHONE: 
     +1 (619) 247-5942\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Trevonne Pinckney (DA85Z5)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160724
    DTSTART;VALUE=DATE:20160722
    UID:cn31z4tbyhod-fz1hn0cqi6yg@airbnb.com
    DESCRIPTION:CHECKIN: 07/22/2016\nCHECKOUT: 07/24/2016\nNIGHTS: 2\nPHONE: 
     +1 (239) 898-9784\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Darren Gooden (CQEBTT)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160803
    DTSTART;VALUE=DATE:20160731
    UID:cn31z4tbyhod-1y3w3tplq0zu@airbnb.com
    DESCRIPTION:CHECKIN: 07/31/2016\nCHECKOUT: 08/03/2016\nNIGHTS: 3\nPHONE: 
     +1 (610) 360-6633\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Chad Opdyke (DFDNMN)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160808
    DTSTART;VALUE=DATE:20160805
    UID:cn31z4tbyhod--h72i8aomz756@airbnb.com
    DESCRIPTION:CHECKIN: 08/05/2016\nCHECKOUT: 08/08/2016\nNIGHTS: 3\nPHONE: 
     +1 (954) 639-3714\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Shay Walsh (5XBTTH)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160815
    DTSTART;VALUE=DATE:20160808
    UID:cn31z4tbyhod-mac5tl2dw3eo@airbnb.com
    DESCRIPTION:CHECKIN: 08/08/2016\nCHECKOUT: 08/15/2016\nNIGHTS: 7\nPHONE: 
     +1 (929) 363-8190\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Akeem Feiton (WYCZSJ)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160821
    DTSTART;VALUE=DATE:20160819
    UID:cn31z4tbyhod-lafyl609ekpi@airbnb.com
    DESCRIPTION:CHECKIN: 08/19/2016\nCHECKOUT: 08/21/2016\nNIGHTS: 2\nPHONE: 
     +1 (646) 523-7594\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Walter Fox (HZA2CZ)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160829
    DTSTART;VALUE=DATE:20160826
    UID:cn31z4tbyhod--ays9sq2g0gtg@airbnb.com
    DESCRIPTION:CHECKIN: 08/26/2016\nCHECKOUT: 08/29/2016\nNIGHTS: 3\nPHONE: 
     +1 (754) 246-2940\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Tamika Jones (ADCFN8)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161008
    DTSTART;VALUE=DATE:20161005
    UID:cn31z4tbyhod--qkhp871f4c32@airbnb.com
    DESCRIPTION:CHECKIN: 10/05/2016\nCHECKOUT: 10/08/2016\nNIGHTS: 3\nPHONE: 
     +1 (425) 891-5016\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Shawn Woods (TSJJCD)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161105
    DTSTART;VALUE=DATE:20161102
    UID:cn31z4tbyhod-j79p4h76jnxy@airbnb.com
    DESCRIPTION:CHECKIN: 11/02/2016\nCHECKOUT: 11/05/2016\nNIGHTS: 3\nPHONE: 
     +1 (281) 710-7868\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Glenn Gilbert (94FNJS)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161110
    DTSTART;VALUE=DATE:20161105
    UID:cn31z4tbyhod-z0uoqh0ijg0k@airbnb.com
    DESCRIPTION:CHECKIN: 11/05/2016\nCHECKOUT: 11/10/2016\nNIGHTS: 5\nPHONE: 
     +1 (830) 456-1207\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Meredith Clements (8CHB29)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161118
    DTSTART;VALUE=DATE:20161111
    UID:cn31z4tbyhod--5el3occjewuc@airbnb.com
    DESCRIPTION:CHECKIN: 11/11/2016\nCHECKOUT: 11/18/2016\nNIGHTS: 7\nPHONE: 
     +1 (925) 519-0481\nEMAIL: (no email alias available)\nPROPERTY: Tropical
      Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Charles Delmolino (ZERBWC)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161129
    DTSTART;VALUE=DATE:20161124
    UID:cn31z4tbyhod--4m4p1x5hmon2@airbnb.com
    DESCRIPTION:CHECKIN: 11/24/2016\nCHECKOUT: 11/29/2016\nNIGHTS: 5\nPHONE: 
     +1 (917) 543-5718\nEMAIL: scott-6e0gjexnbdo3gktl@guest.airbnb.com\nPROPE
     RTY: Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Scott Reale (5BWZC4)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161205
    DTSTART;VALUE=DATE:20161202
    UID:cn31z4tbyhod-hhhkyes8uijs@airbnb.com
    DESCRIPTION:CHECKIN: 12/02/2016\nCHECKOUT: 12/05/2016\nNIGHTS: 3\nPHONE: 
     +1 (914) 960-7912\nEMAIL: lindsay-lnnahjxv7mtalxj7@guest.airbnb.com\nPRO
     PERTY: Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Lindsay Klein (9D8PJ8)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20170102
    DTSTART;VALUE=DATE:20161226
    UID:cn31z4tbyhod-oou2j35paua6@airbnb.com
    DESCRIPTION:CHECKIN: 12/26/2016\nCHECKOUT: 01/02/2017\nNIGHTS: 7\nPHONE: 
     +506 8 868 9215\nEMAIL: adriana-70yuldfuvz6exlda@guest.airbnb.com\nPROPE
     RTY: Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Adriana Quiros (NE3RFN)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20170111
    DTSTART;VALUE=DATE:20170104
    UID:cn31z4tbyhod-firr1585xa7w@airbnb.com
    DESCRIPTION:CHECKIN: 01/04/2017\nCHECKOUT: 01/11/2017\nNIGHTS: 7\nPHONE: 
     +1 (514) 927-4600\nEMAIL: marcel-rwupwcvc6d6vv556@guest.airbnb.com\nPROP
     ERTY: Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Marcel Groleau (9HRSXZ)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20170121
    DTSTART;VALUE=DATE:20170117
    UID:cn31z4tbyhod-qyvl6rznyesq@airbnb.com
    DESCRIPTION:CHECKIN: 01/17/2017\nCHECKOUT: 01/21/2017\nNIGHTS: 4\nPHONE: 
     +1 (505) 321-9458\nEMAIL: sergio-23nza8uyfzu8sb98@guest.airbnb.com\nPROP
     ERTY: Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Sergio Tirado (RM54BM)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20170305
    DTSTART;VALUE=DATE:20170216
    UID:cn31z4tbyhod--7eof74b6h3q6@airbnb.com
    DESCRIPTION:CHECKIN: 02/16/2017\nCHECKOUT: 03/05/2017\nNIGHTS: 17\nPHONE:
      +33 6 76 04 11 95\nEMAIL: bernard-1x4rpabhk5j9yzs3@guest.airbnb.com\nPR
     OPERTY: Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach\n
    SUMMARY:Bernard Elmalem (K5MF5W)
    LOCATION:Tropical Retreat- 1.5 mi to Las Olas Blvd &amp; Beach
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160711
    DTSTART;VALUE=DATE:20160708
    UID:-sg3p5bpek2gn-x055w817b23p@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160725
    DTSTART;VALUE=DATE:20160724
    UID:-sg3p5bpek2gn-xobd7jo6cv1t@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160731
    DTSTART;VALUE=DATE:20160728
    UID:-sg3p5bpek2gn-24l1rot6mqtt@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160819
    DTSTART;VALUE=DATE:20160816
    UID:-sg3p5bpek2gn--pih0m6c16ygf@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160823
    DTSTART;VALUE=DATE:20160822
    UID:-sg3p5bpek2gn--47s532pniut7@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20160925
    DTSTART;VALUE=DATE:20160829
    UID:-sg3p5bpek2gn--7tcr0ezisq1j@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161003
    DTSTART;VALUE=DATE:20160929
    UID:-sg3p5bpek2gn-w06qr66c509x@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161005
    DTSTART;VALUE=DATE:20161004
    UID:-sg3p5bpek2gn--ixkq49h4tbs3@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161024
    DTSTART;VALUE=DATE:20161008
    UID:-sg3p5bpek2gn--gtx4mmcry3sv@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161102
    DTSTART;VALUE=DATE:20161025
    UID:-sg3p5bpek2gn-cylycusbl2jh@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20161130
    DTSTART;VALUE=DATE:20161129
    UID:-sg3p5bpek2gn--yx25kuc3pp67@airbnb.com
    SUMMARY:Not available
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20170128
    DTSTART;VALUE=DATE:20170122
    UID:-sg3p5bpek2gn--34z0d6olbk3f@airbnb.com
    SUMMARY:FlipKey reservation (Andy Vitrone). Move from 628 to 632A.
    END:VEVENT
    BEGIN:VEVENT
    DTEND;VALUE=DATE:20171223
    DTSTART;VALUE=DATE

:20171222
UID:-sg3p5bpek2gn-4mjfwyuewkk1@airbnb.com
SUMMARY:Not available
END:VEVENT
END:VCALENDAR
</code></pre>

<p>=-=-=-=-=-=-=-=-=</p>

<p>My Bad, i did not explained whole scenario.
Why i want to merge two ics, is because, i am using wordpress with icalendar.
and i get reservation dates from different sites like "Airbnb".</p>

<p>but when wordpress import first calendar, and after that i import second calendar, it overrides the first one.</p>

<p>So what i am trying to do.. is merge calendar events and then import all the events all together.</p>

<p>Here is the PHP Code so far for iCalendar.php.</p>

<p>i have edited this code, tried to do foreach loop and used and moved process to another extended function for processing.</p>

<pre><code>function wpestate_import_calendar_feed_listing($prop_id){
    $property_icalendar_import =   get_post_meta($prop_id, 'property_icalendar_import', true);
    $property_icalendar_import2 =   get_post_meta($prop_id, 'property_icalendar_import2', true);
    $iCalURLS = array(
        'property_icalendar_import' =&gt; $property_icalendar_import,
        'property_icalendar_import2' =&gt; $property_icalendar_import2
    );
    wpestate_import_calendar_feed_listing_extend($prop_id, $iCalURLS);
//    wpestate_import_calendar_feed_listing_extend($prop_id, $property_icalendar_import2);


}

function wpestate_import_calendar_feed_listing_extend($prop_id,$iCalURLS){


    if(!intval($prop_id)){
        exit();
    }


    if(empty($iCalURLS) || !is_array($iCalURLS) || (empty($iCalURLS['property_icalendar_import']) and empty($iCalURLS['property_icalendar_import2']))){
        return;
    }

    //Validate the URLS,only if they are not empty.
    foreach($iCalURLS as $iCalURL){
        if(!empty($iCalURL)){
            if (filter_var($iCalURL, FILTER_VALIDATE_URL) === FALSE) {
                return;
            }
        }
    }

    foreach($iCalURLS as $iCalURL){

    }

    $ical   = new ICal($iCalURLS['property_icalendar_import']);
    $ical   = new ICal($property_icalendar_import);

    $events = $ical-&gt;events();
    $date = $events[0]['DTSTART'];



    if (!is_array($events)){
        return;
    }

    wpestate_clear_ical_imported($prop_id);


    //DTSTART which sets a starting time, and a DTEND which sets an ending time.
    foreach ($events as $event) {
        $unix_time_start    ='';
        $unix_time_end      ='';
        if( isset($event['UID']) ){
            $uid                =$event['UID'];

        }else{
            $uid=   esc_html__('external','wpestate');
        }


        if( isset($event['DTSTART']) ){
            //  echo "DTSTART: ".$event['DTSTART']." - UNIX-Time: ".$ical-&gt;iCalDateToUnixTimestamp($event['DTSTART'])."&lt;br/&gt;";
            $unix_time_start =$ical-&gt;iCalDateToUnixTimestamp($event['DTSTART']);
        }

        if( isset($event['DTEND']) ){
            //echo "DTEND: ".$event['DTEND']."&lt;br/&gt;";
            $unix_time_end =$ical-&gt;iCalDateToUnixTimestamp($event['DTEND']);
       }

        /*
        if( isset($event['SUMMARY']) ){
         echo "SUMMARY: ".$event['SUMMARY']."&lt;br/&gt;";
        }

        if( isset($event['DTSTAMP']) ){
            echo "DTSTAMP: ".$event['DTSTAMP']."&lt;br/&gt;";
        }

        if( isset($event['UID']) ){
            echo "UID: ".$event['UID']."&lt;br/&gt;";
        }

        if( isset($event['CREATED']) ){
            echo "CREATED: ".$event['CREATED']."&lt;br/&gt;";
        }

        if( isset($event['DESCRIPTION']) ){
        echo "DESCRIPTION: ".$event['DESCRIPTION']."&lt;br/&gt;";
        }

        if( isset($event['LAST-MODIFIED']) ){
            echo "LAST-MODIFIED: ".$event['LAST-MODIFIED']."&lt;br/&gt;";
        }

        if( isset($event['LOCATION']) ){
            echo "LOCATION: ".$event['LOCATION']."&lt;br/&gt;";
        }

        if( isset($event['SEQUENCE']) ){
            echo "SEQUENCE: ".$event['SEQUENCE']."&lt;br/&gt;";
        }

        if( isset($event['STATUS']) ){
            echo "STATUS: ".$event['STATUS']."&lt;br/&gt;";
        }

        if( isset($event['TRANSP']) ){
            echo "TRANSP: ".$event['TRANSP']."&lt;br/&gt;";
        }

        echo "&lt;hr/&gt;";

        */

       // print $unix_time_start." / ".$unix_time_end." / ".$uid." &lt;/br&gt;";
        if( $unix_time_start!='' &amp;&amp; $unix_time_end!='' &amp;&amp; $uid !=''){
            wpestate_insert_booking_external_event($prop_id, $unix_time_start,$unix_time_end,$uid);
        }
    }

   // $reservation_array = get_post_meta($prop_id, 'booking_dates',true);
    //print_R($reservation_array);
}
</code></pre>