我有下面的代码来创建一个范围内的日期列表。如果我提供一周中的某一天,它将获得范围内thoes日期的所有日期。如果我不这样做,它会变得整天都是。
我的问题是,从10月24日到29日,它会多返回一天。当我添加额外的代码来获取一周中某一天的日期时,我注意到了之前的情况。
我认为问题行是array_push($aryRange,date('Y-m-d',$iDateFrom));//第一个条目
function createDateRangeArray($strDateFrom,$strDateTo,$dateOfWeek=null) {
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2),substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom) {
if(!isset($dateOfWeek)) {
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo) {
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
} else {
while ($iDateFrom<$iDateTo) {
if(date('w',$iDateFrom)==$dateOfWeek)
array_push($aryRange,date('Y-m-d',$iDateFrom));
$iDateFrom+=86400; // add 24 hours
}
}
}
if(count($aryRange)<1){
return false;
} else{
return $aryRange;
}
}发布于 2011-10-11 03:24:33
使用strtotime()函数不是更简单吗?http://php.net/manual/en/function.strtotime.php
$iDateFrom = strtotime("+1 day", $iDateFrom); // add 1 day发布于 2011-10-11 03:15:07
你必须考虑到daylight saving time。
发布于 2011-10-11 03:15:47
你每次都会增加24小时。这与添加一天不完全是一回事。我强烈怀疑你所在的时区时钟会回到10月23日或10月30日,这会打乱你的计算。
我不完全清楚对你来说最好的解决方案是什么,但希望解释一下正在发生的事情将是一个好的开始……
https://stackoverflow.com/questions/7717468
复制相似问题