PHP中的日期操作
我试图在一个循环中重复一个月的日期,重复固定次数。如果日期是有效日期,它将只回显该日期,否则它将跳过该日期。 例如,如果“2012-02-30 10:10:00”出现在循环中,它将被忽略和跳过,并将进入下一个迭代 以下代码适用于开始日期“2011-11-07 10:15:00”,但当我将其更改为注释为“2011-11-30 10:15:00”的开始日期时,它会在跳过一次迭代后增加24小时 我不确定我会错在哪里PHP中的日期操作,php,date,datetime,time,date-math,Php,Date,Datetime,Time,Date Math,我试图在一个循环中重复一个月的日期,重复固定次数。如果日期是有效日期,它将只回显该日期,否则它将跳过该日期。 例如,如果“2012-02-30 10:10:00”出现在循环中,它将被忽略和跳过,并将进入下一个迭代 以下代码适用于开始日期“2011-11-07 10:15:00”,但当我将其更改为注释为“2011-11-30 10:15:00”的开始日期时,它会在跳过一次迭代后增加24小时 我不确定我会错在哪里 <?php //$start_date = "2011-11-30 10:15:
<?php
//$start_date = "2011-11-30 10:15:00";
$start_date = "2011-11-07 10:15:00";
$no_of_repeat = 15;
$repeat_timing = 0;
for($x=0; $x<$no_of_repeat; $x++) {
$start_date = date('Y-m-d G:i:s',(strtotime($start_date) + ($repeat_timing)));
if(date("m",strtotime($start_date)) != 12) {
$next_month = date('m',strtotime($start_date)) + 1;
$year = date('Y',strtotime($start_date));
} else {
$next_month = 1 ;
$year = date('Y',strtotime($start_date)) + 1;
}
$day = date("d",strtotime($start_date));
$next_date =
$year."-".
$next_month."-".
$day." ".
date("G",strtotime($start_date)).":".
date("i",strtotime($start_date)).":".
date("s",strtotime($start_date));
if(checkdate($next_month,$day,$year))
$repeat_timing = strtotime($next_date) - strtotime($start_date);
else
continue;
echo $next_date."<br />";
}
?>
当你打电话的时候
$start_date = date('Y-m-d G:i:s',(strtotime($start_date) + ($repeat_timing)));
它在日期上增加了1个月,但也补偿了重复时间变量。您在2011年1月30日的日期上增加了2678400秒,大约相当于2011年3月1日
最好将月、日和年与日期变量分开,并手动进行计算。这将防止日期更改您的时间范围。尝试以下操作:
// starting variables
$startDate = "2011-11-30";
$time = '10:15:00';
$numberOfTimesToRepeat = 10;
// Set our counts to 0
$count = 0;
$datesFound = 0;
// parse date
list($startYear,$startMonth,$startDay) = explode('-',$startDate);
// Create an array
while ($datesFound < $numberOfTimesToRepeat) {
// Calculate number of months to add
$monthsToAdd = fmod($count,12);
// Calculate new month number
$newMonth = (($startMonth + $monthsToAdd) > 12) ? ($startMonth + $monthsToAdd - 12) : ($startMonth + $monthsToAdd);
// Add the leading 0 if necessary
if ($newMonth < 10 && strlen($newMonth) < 2) $newMonth = '0'.$newMonth;
// Calculate number of months to add
$yearsToAdd = floor(($count/12));
$newYear = $startYear + $yearsToAdd;
if (checkdate($newMonth,$startDay,$newYear)) {
$dates[] = $newYear.'-'.$newMonth.'-'.$startDay.' '.$time;
$datesFound++;
}
// increase our count either way
$count++;
}
// Show the dates
foreach ($dates as $date) {
echo $date.'<br />';
}
//开始变量
$startDate=“2011-11-30”;
$time='10:15:00';
$numberOfTimesToRepeat=10;
//将计数设置为0
$count=0;
$datesFound=0;
//解析日期
列表($startYear,$startMonth,$startDay)=分解('-',$startDate);
//创建一个数组
而($datesFound<$numberOfTimesToRepeat){
//计算要添加的月数
$monthsToAdd=fmod($count,12);
//计算新月数
$newMonth=($startMonth+$monthstoad)>12)?($startMonth+$monthstoad-12):($startMonth+$monthstoad);
//如有必要,添加前导0
如果($newMonth<10&&strlen($newMonth)<2)$newMonth='0'。$newMonth;
//计算要添加的月数
$yearsToAdd=楼层($count/12));
$newYear=$startYear+$yearsToAdd;
if(检查日期($newMonth,$startDay,$newYear)){
$dates[]=$newYear.'-'.$newMonth.'-'.$startDay.'.$time;
$datesFound++;
}
//不管怎样,增加我们的数量
$count++;
}
//显示日期
foreach($日期作为$日期){
回显$date.“
”;
}
这里有一个简短、可靠的函数,可以响应有效的日期戳:
你展示了预期的结果,但你得到的结果是什么?你能发布它吗?只需执行上面给出的脚本,取消注释开始日期并注释当前开始日期_date@NikhilRao如果您认为这是列表中的最佳答案,请将其授予绿色勾号,以便将您的问题从未回答问题列表中删除。如果有不太对劲的地方,请告诉我,我会调整的。
// starting variables
$startDate = "2011-11-30";
$time = '10:15:00';
$numberOfTimesToRepeat = 10;
// Set our counts to 0
$count = 0;
$datesFound = 0;
// parse date
list($startYear,$startMonth,$startDay) = explode('-',$startDate);
// Create an array
while ($datesFound < $numberOfTimesToRepeat) {
// Calculate number of months to add
$monthsToAdd = fmod($count,12);
// Calculate new month number
$newMonth = (($startMonth + $monthsToAdd) > 12) ? ($startMonth + $monthsToAdd - 12) : ($startMonth + $monthsToAdd);
// Add the leading 0 if necessary
if ($newMonth < 10 && strlen($newMonth) < 2) $newMonth = '0'.$newMonth;
// Calculate number of months to add
$yearsToAdd = floor(($count/12));
$newYear = $startYear + $yearsToAdd;
if (checkdate($newMonth,$startDay,$newYear)) {
$dates[] = $newYear.'-'.$newMonth.'-'.$startDay.' '.$time;
$datesFound++;
}
// increase our count either way
$count++;
}
// Show the dates
foreach ($dates as $date) {
echo $date.'<br />';
}
function getValidDateTimes($date,$iterations){
if(preg_match("/(\d{4})-(\d{1,2})-(\d{2}) (.*)/",$date,$n)){ // capture datetime bits
list(,$Y,$n,$d,$time)=$n; // declare bits as variables for readability
for(++$n,$x=0; $x<$iterations; ++$x,++$n){ // start with month after $start_date
$n=($n>12?"1":$n); // reset to 1 when month exceeds 12
$date="$Y-$n-$d"; // build literal new date
if($date==date("Y-n-d",strtotime($date))){ // built date versus assumed date
echo "$date $time<br>"; // store valid date with concatenated time
}
}
}
}
$start_date="2011-11-30 10:15:00";
$iterations=15;
getValidDateTimes($start_date,$iterations);
2011-12-30 10:15:00
2011-1-30 10:15:00
2011-3-30 10:15:00
2011-4-30 10:15:00
2011-5-30 10:15:00
2011-6-30 10:15:00
2011-7-30 10:15:00
2011-8-30 10:15:00
2011-9-30 10:15:00
2011-10-30 10:15:00
2011-11-30 10:15:00
2011-12-30 10:15:00
2011-1-30 10:15:00