简体   繁体   中英

PHP Request time out on simple function

I have this function which calculates 'Book Value' of fixed assets using data from the depreciation table, Ii is working fine on localhost but when I tried to go on production, am getting 'request time out' after taking long time to load, I think I messed up with looping or something and It will be great if anyone suggest code optimization or hack to make this function faster.

I tried to limit the number of records to loop and It worked but unfortunately, I have over 20,000 data to process.

    $cat = null;
    $total_a = null;
    $assets = $this->Depreciation->get_all();

    if (!empty($assets)) {
        foreach ($assets as $asset) {

            $cost = $asset->cost + $asset->add_cost + $asset->adj_cost;
            $salve = $asset->salvage_value;
            $life = $asset->life;
            $life_t = 0;
            $counter = 0;
            $date1 = date('Y-12-31');
            $date2 = $asset->in_service;

            $diff = abs(strtotime($date2) - strtotime($date1));

            $years = floor($diff / (365 * 60 * 60 * 24));
            $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));

            $in_service_d = date('Y-12-31');


            if ($asset->rev > 0) {
                $cost = $asset->rev;
            }


            $dep1 = ($cost - $salve) / $life * ($months / 12);
            $dep = ($cost - $salve) / $life;


            $total = 0;
            $year = 1;


            for (; $cost > $salve;) {
                if ($year == 1 && $months < 12) {
                    $life_t++;
                    $year++;
                    $cost = $cost - $dep1;
                    $counter++;
                    $total = $dep1 + $total;

                    if($in_service_d == date('Y-12-31')){
                        $total_a = $total_a + $cost;
                    }

                    $in_service_d = date('Y-12-31', strtotime("+12 months", strtotime($in_service_d)));


                } elseif ($year != 1) {
                    $life_t++;
                    if ($life_t > $life) {
                        $dep = $dep - $dep1;
                    }
                    $cost = $cost - $dep;
                    $counter++;
                    $total = $dep + $total;

                    if($in_service_d == date('Y-12-31')){
                        $total_a = $total_a + $cost;
                    }

                    $in_service_d = date('Y-12-31', strtotime("+12 months", strtotime($in_service_d)));

                }

            }

        }

    }

     return $total_a;

Your friend is the set_time_limit() function.

I had a similar script of exporting a formatted Excel file from a large database. The script would always run out of time whenever it exceeded over 1,000 rows.

In the loop section of my logic, I set up the following:

set_time_limit(30);

Which would extend the allowed time to run for another 30 seconds from that point onward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM