2017-06-15 53 views
0
<?php 
    $today = "2017-06-14"; 
    $prev = "1994-06-01"; 
    $date = $prev + //keep on adding 60 days till june,2017 
    echo '<br>' .$date; 
?> 

我還想在數據庫中更新1000個條目的日期!我想有一個高效,更少的時間消耗方法?通過將x天添加到上一日期來查找確切日期

+0

數學會在這裏工作。找到差異b/w 2日期,除以差異。根據你的設置例如:today-prev = diff;所以diff/number_of_set將會是你的x – Santosh

+0

你想每一天或最後一天爲每個條目 –

+0

最後日期,即什麼是最近的日期! –

回答

0

以下是我將如何做到這一點......但有一點值得關注,那就是您應該計劃處理 - 如果60天的增量超過2017年的6月會怎樣?我已成立了一個演示揭露這種情況的發生:

碼(Demo):

$today=strtotime('today'); 
//$min=strtotime('first day of this month'); 
$max=strtotime('last day of this month'); 

$prevs=['1994-06-01','1994-06-7','1994-06-14','1994-06-15','1994-06-17','1994-06-21','1994-06-30']; 
foreach($prevs as $prev){ 
    echo "prev = ",$prev = strtotime($prev),"\n"; 
    echo "diff = ",$diff=$today-$prev,"\n"; // calculate seconds between two dates 
    // add just enough seconds (in 60 day increments) to reach at least $min 
    echo "newseconds = ",$newseconds=$prev+5184000*ceil($diff/5184000),"\n"; // 60 days in seconds 
    if($newseconds>$max){echo "Warning: new incremented date not in input month\n";} // handle this exception? 
    echo "newdate = ",date("Y-m-d",$newseconds),"\n\n"; 
} 

輸出:

prev = 770454000 
diff = 727056000 
newseconds = 1501398000 
Warning: new incremented date not in input month 
newdate = 2017-07-30 

prev = 770972400 
diff = 726537600 
newseconds = 1501916400 
Warning: new incremented date not in input month 
newdate = 2017-08-05 

prev = 771577200 
diff = 725932800 
newseconds = 1502521200 
Warning: new incremented date not in input month 
newdate = 2017-08-12 

prev = 771663600 
diff = 725846400 
newseconds = 1502607600 
Warning: new incremented date not in input month 
newdate = 2017-08-13 

prev = 771836400 
diff = 725673600 
newseconds = 1497596400 
newdate = 2017-06-16 

prev = 772182000 
diff = 725328000 
newseconds = 1497942000 
newdate = 2017-06-20 

prev = 772959600 
diff = 724550400 
newseconds = 1498719600 
newdate = 2017-06-29