需要php优化:日期范围->;月
(现已修复,工作正常,但如果仍有人想要重构,请留下备注) 这是一个精简版的函数,我有一个迭代的日期范围,并分配一个唯一的整数给每个 在处理大型数据集时,在不同的日期范围内多次运行此脚本,我会遇到一个致命错误,为脚本分配了太多内存,脚本在这个循环中死亡需要php优化:日期范围->;月,php,optimization,date-range,Php,Optimization,Date Range,(现已修复,工作正常,但如果仍有人想要重构,请留下备注) 这是一个精简版的函数,我有一个迭代的日期范围,并分配一个唯一的整数给每个 在处理大型数据集时,在不同的日期范围内多次运行此脚本,我会遇到一个致命错误,为脚本分配了太多内存,脚本在这个循环中死亡 致命错误:已耗尽268435456字节的允许内存大小 修复了迭代过程中未考虑潜在夏令时的问题 所以,我想知道是否有人可以推荐一种更理想的方法来生成这个月/整数列表 它必须允许我以我喜欢的任何数字开始Int,并且 <?php // upda
致命错误:已耗尽268435456字节的允许内存大小
修复了迭代过程中未考虑潜在夏令时的问题
所以,我想知道是否有人可以推荐一种更理想的方法来生成这个月/整数列表
它必须允许我以我喜欢的任何数字开始Int,并且
<?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;
}
?>
由于节省了额外的一小时光照时间,您的函数似乎处于无休止的循环中
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
两者都将输出2010-10-31 02:00:00
。因此,STROTIME('2010-10-31')+86400实际上是2010-10-31 23:00:00
,但不是第二天
因此,您应该添加86400多秒,以确保切换到第二天:-)我认为您的数组索引太疯狂了
即:
我会将你的“$monthInt=“Month#-”.str_pad($monthInt,3,“0”,str_pad_LEFT);”行移到循环外
<?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;
}?>
虽然使用新的DateInterval
类等可能会更好,但这似乎是可行的,我严重怀疑这是内存占用,问题可能出在代码的另一部分(使用xdebug
&启用内存增量以获得更好的概述)试一试那一个绝妙的-这正是问题所在。。。而不是增加86400秒。。。我现在检查月份并不断递增,直到它发生变化。。。不再超时。。。!仅供参考-我还发现这个库非常易于使用和可靠:只需使用标准的cron通知,它将进入下一个匹配的可操作阶段--使生成选项列表变得非常容易…你是对的,但这只是因为我正在剥离一些东西来传达这个函数。。。发现这是夏令时的问题。。。更新函数以更正错误。
<?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;
}?>