u01jmg3 / ics-parser

Parser for iCalendar Events • PHP 8+, 7 (≥ 7.4), 5 (≥ 5.6)
MIT License
450 stars 144 forks source link

How to quickly sum days of events in a time range? #198

Closed FRDS closed 5 years ago

FRDS commented 5 years ago

Description of the Issue:

I want to quickly sum days of events in a time range (i.e month of October 2018). I've tried to use the numberofDays method but I don't actually know how to use it :

Without knowing the answers I've tried it anyway, getting this: Uncaught Error: Call to protected method ICal\ICal::numberofDays() from context

Here's the ICS file URL (it can't be used for initURL, so you need to download it first) : https://www.airbnb.com/calendar/ical/2051868.ics?s=0624fe394b3084d9f07ae453082c76e9

Code that I've tested:

$events = $ical->eventsFromRange('2018-12-01 00:00:00', '2018-12-31 23:59:59');
                foreach ($events as $event) :
                    $day = $ical->numberofDays($event->$dtstart_array[2],$event->$dtend_array[2]);
                    $days += $day;
                endforeach
var_dump($days);
u01jmg3 commented 5 years ago

I am not sure you can have tested the code provided.


As you have Carbon at your disposal I would do the following.

<?php

use Carbon\Carbon;

// Load iCal...

$events = $ical->eventsFromRange('2018-12-01 00:00:00', '2018-12-31 23:59:59');
$days   = 0;

foreach ($events as $event) :
    $end   = Carbon::createFromTimestamp($event->dtend_array[2]);
    $start = Carbon::createFromTimestamp($event->dtstart_array[2]);
    $days += $end->diffInDays($start);
endforeach;

var_dump($days);