首页 > 解决方案 > 带有 iCal Parser 的 PHP 函数减慢页面加载时间

问题描述

我有这个使用 Martin Thoma Github Link Here的 iCal 解析器的函数

此功能执行非常缓慢,大约需要 12 秒才能从 iCal 日历中获取事件日期。

如何改进功能流程?

excludes 变量然后在我的 Javascript 加载中输出到页面底部的结束 body 标记之前。

我试过没有这个功能的页面,它加载得很快,所以我把它缩小到这个功能。

可能是我错误地重用了 iCal 类,但我不知道如何解决这个问题。

请帮忙

function process_ical($airbnb_ical_link,$google_ical_link,$booking_dot_com_ical_link,$homeaway_ical_link) {

    require 'class.iCalReader.php';

    $exclusions = '"2019-01-01"';

    if ($airbnb_ical_link != '') {

        $ical   = new ICal($airbnb_ical_link);
        $events = $ical->events();

        if (is_array($events) || is_object($events)) {

            foreach ($events as $event) {
                $formatedStart = $ical->iCalDateToUnixTimestamp($event['DTSTART']);
                $formatedEnd = $ical->iCalDateToUnixTimestamp($event['DTEND']);
                $str1 = date('Y-m-d',$formatedStart);
                $str2 = date('Y-m-d',$formatedEnd);
                $date_from = strtotime($str1);
                $date_to = strtotime($str2);
                $date_to = ($date_to - 86400); // Fix the extra day issue
                    for ($i=$date_from; $i<=$date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }
    }

    if ($google_ical_link != '') {

        $ical2   = new ICal($google_ical_link);
        $events2 = $ical2->events();

        if (is_array($events2) || is_object($events2)) {

            foreach ($events2 as $event2) {
                $formatedStart2 = $ical2->iCalDateToUnixTimestamp($event2['DTSTART']);
                $formatedEnd2 = $ical2->iCalDateToUnixTimestamp($event2['DTEND']);
                $strr1 = date('Y-m-d',$formatedStart2);
                $strr2 = date('Y-m-d',$formatedEnd2);
                $google_date_from = strtotime($strr1);
                $google_date_to = strtotime($strr2);
                $google_date_to = ($google_date_to - 86400); // Fix the extra day issue
                    for ($i=$google_date_from; $i<=$google_date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }

    }

    if ($booking_dot_com_ical_link != '') {

        $ical3   = new ICal($booking_dot_com_ical_link);
        $events3 = $ical3->events();

        if (is_array($events3) || is_object($events3)) {

            foreach ($events3 as $event3) {
                $formatedStart3 = $ical3->iCalDateToUnixTimestamp($event3['DTSTART']);
                $formatedEnd3 = $ical3->iCalDateToUnixTimestamp($event3['DTEND']);
                $str1_priceline = date('Y-m-d',$formatedStart3);
                $str2_priceline = date('Y-m-d',$formatedEnd3);
                $priceline_date_from = strtotime($str1_priceline);
                $priceline_date_to = strtotime($str2_priceline);
                $priceline_date_to = ($priceline_date_to - 86400); // Fix the extra day issue
                    for ($i=$priceline_date_from; $i<=$priceline_date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }
    }

    if ($homeaway_ical_link != '') {

        $ical4   = new ICal($homeaway_ical_link);
        $events4 = $ical4->events();

        if (is_array($events4) || is_object($events4)) {

            foreach ($events4 as $event4) {
                $formatedStart4 = $ical4->iCalDateToUnixTimestamp($event4['DTSTART']);
                $formatedEnd4 = $ical4->iCalDateToUnixTimestamp($event4['DTEND']);
                $str1_homeaway = date('Y-m-d',$formatedStart4);
                $str2_homeaway = date('Y-m-d',$formatedEnd4);
                $homeaway_date_from = strtotime($str1_homeaway);
                $homeaway_date_to = strtotime($str2_homeaway);
                $homeaway_date_to = ($homeaway_date_to - 86400); // Fix the extra day issue
                    for ($i=$homeaway_date_from; $i<=$homeaway_date_to; $i+=86400) {
                        $exclusions .= date(',"Y-m-d"', $i);
                    }
            }

        }
    }

    return $exclusions;

}

标签: phpperformancefunctionreturnicalendar

解决方案


推荐阅读