开发者

php optimization needed: date range -> months

(fixed and working fine now, but if anyone still wants to refactor, leave a note)

This is a stripped down version of a function I have which iterates over a date range and assigns a unique integer to each...

When working with large datasets, running this several times over different date开发者_JAVA百科 ranges, I'm getting a fatal error, assigning too much memory to the script and it dies in this loop...

Fatal error: Allowed memory size of 268435456 bytes exhausted

fixed, was an issue with the iteration not taking into account the potential daylight-savings-time

So, I was wondering if someone could recommend a more optimal way of generating this list of months/ints...

It must allow me to start the Int at whatever number I like and

<?php
// updated: 2010.11.04 with Qwerty's recommendations 
//  for fixing daylight savings time issue
function monthIterate($monthInt, $startDate, $stopDate) {
    $epoch = $startMain = strtotime($startDate);
    $stopMain = strtotime($stopDate);
    while ($epoch <= $stopMain) {
        // get the start/stop dates for "this month"
        $start = date("Y-m-01", $epoch);
        $stop = date("Y-m-t", $epoch);
        // uniqueID for the month
        $monthKey = "Month#-".str_pad($monthInt, 3, "0", STR_PAD_LEFT);
        $months[$monthKey] = compact('start', 'stop');
        // move forward in loop, +1 day should get us to the next month
        $epoch = strtotime($stop);
        $currentMonth = $nextMonth = date('m', $epoch);
        while ($currentMonth == $nextMonth) {
            $epoch = $epoch + 86400;
            $nextMonth = date('m', $epoch);
        }
        $monthInt++;
    }
    return $months; 
}
?>


Looks like your function goes in endless loop because of extra hour in light saving time.

echo date('Y-m-d H:i:s', strtotime('2010-10-31') + 60*60*2); // adding 2 hours
echo date('Y-m-d H:i:s', strtotime('2010-10-31') + 60*60*3); // adding 3 hours

both will output 2010-10-31 02:00:00. Thus strtotime('2010-10-31') + 86400 is actually 2010-10-31 23:00:00, but not next day.

So you should add more than 86400 seconds to be sure you switched to next day :-)


I think your array index gets too crazy.

ie :

[Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-Month#-123] => Array
(
    [start] => 2011-12-01
    [stop] => 2011-12-31
)

I would move your "$monthInt = "Month#-".str_pad($monthInt, 3, "0", STR_PAD_LEFT);" lines outside your loop.

<?php
function monthIterate($monthInt, $startDate, $stopDate) {

$monthInt = "Month#-".str_pad($monthInt, 3, "0", STR_PAD_LEFT); <--

$epoch = $startMain = strtotime($startDate);
$stopMain = strtotime($stopDate);
while ($epoch <= $stopMain) {
    // get the start/stop dates for "this month"
    $start = date("Y-m-01", $epoch);
    $stop = date("Y-m-t", $epoch);
    // uniqueID for the month
    $months[$monthInt] = compact('start', 'stop');
    // move forward in loop, +1 day should get us to the next month
    $epoch = strtotime($stop) + 86400;
    $monthInt++;
}
return $months; 
}?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜