2011-11-07 62 views
1

我想在一個循環中重複一個月的固定次數。如果日期是有效日期,它只會回顯日期,否則會跳過日期。 例如,如果「2012-02-30 10:10:00」正在循環中,它將被忽略並跳過,並且它將轉到下一次迭代。在PHP中處理日期

下面的代碼適用於start_date「2011-11-07 10:15:00」,但是當我將其更改爲start_date,即「2011-11-30 10:15:00」它添加跳過一個迭代24小時後。

我不知道我哪裏出錯了。

<?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 />"; 
} 
?> 

的評論起始日期預期結果如下:

2011-12-30 10:15:00 
2012-1-30 10:15:00 
2012-3-30 10:15:00 
2012-4-30 10:15:00 
2012-5-30 10:15:00 
2012-6-30 10:15:00 
2012-7-30 10:15:00 
2012-8-30 10:15:00 
2012-9-30 10:15:00 
2012-10-30 10:15:00 
2012-11-30 10:15:00 
2012-12-30 10:15:00 
2013-1-30 10:15:00 
+0

您顯示預期的結果,但是您得到的結果是什麼?你可以發佈嗎? – DesignerGuy

+0

只需執行上面給出的腳本並解開註釋的start_date並評論當前的start_date –

回答

0

當你調用

$start_date = date('Y-m-d G:i:s',(strtotime($start_date) + ($repeat_timing))); 

這是增加1個月的時間,但它也補償repeat_timing變量。您將在2011年1月30日添加2678400秒,這相當於2011年3月1日左右。

您最好將月份,日期和年份與日期變量分開並手動進行計算。這將使DATE不會改變你的時間表。

0

試用下列:

// 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 />'; 
} 
0

這裏是一個短的,值得信賴函數相呼應有效的datetime郵票:(如所期望/預期)online demo

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 
+0

@NikhilRao如果您認爲這是列表中的最佳答案,請給它一個綠色的勾號,以便您的問題從未答覆問題列表。如果有什麼不對,請讓我知道,我會調整它。 – mickmackusa