2014-07-19 52 views
0

我有一個起始日期2014年7月18日一樣,一個結束日期像2014年7月24日,並在一段時間:每2天。 因此,從07/18開始的每兩天,我都會警告用戶必須做的事情,如果不是正確的一天,我會警告用戶有關剩餘天數的情況。 我該怎麼辦?如果再添加時間和每2小時一段時間?如何在php中管理日期序列以顯示下一個日期還剩多少天?

我想了解商店第一所有的日期在陣列警告[07-18/2014,二零一四年七月二十零日,2014年7月22日,2014年7月24日]

我的代碼是下面的但它不起作用。也許這是不正確的,像我使用strtotime

$endg = strtotime ("+". $dataupto . " days", strtotime ($row['startdate'])) ;  
$endg = date ('Y/m/d' , $endg); 
$endgSTR = strtotime($endg); 
$tempg = strtotime($row['startdate']); 

while($tempg < $endgSTR){ 

     $arraymonitor[] = strtotime( date($tempgdate) ); 

     $tempg = $tempg + (strtotime (" +1 days", $tempg )); 


     $arraymonitor[] = $tempg; 
     echo "</br> tempg in while:"; 
     echo $tempg . " "; 

     echo "</br> tempg in while 2:"; 
     echo date ('Y/m/d' , $tempg) . "<br/>";      


    } 

我也接受其他建議!

Update解決方案

   header("content-type: text-plain"); 
       function dayDiff($start, $end){ 
        $timeleft = $end - $start; 
        $daysleft = round((($timeleft/24)/60)/60); 
        return $daysleft; 
       } 

       function testWarning($today, $end, $delay){ 
        $endDate  = strtotime($end); 
        $warningDate = $endDate; 
        $todayDate = strtotime($today); 

        if($todayDate == $warningDate){ 
         echo "Oggi c'è un controllo da fare"; 
        }elseif($todayDate < $warningDate){ 
         echo "Miss " . dayDiff($todayDate, $warningDate) . " days"; 
        }else{ 
         echo "warning was " . abs(dayDiff($todayDate, $warningDate)) . " giorni fa"; 
        } 

        echo"\n"; 
       } 

       $ardata = [07/18/2014, 07/20/2014, 07/22/2014, 07/24/2014]; 
       $today3 = "07/21/2014"; // the day after warning 
       testWarning($today3, $end, $delay); 

       $diffmin = 1000;           
       for ($i = 0 ; $i<= count($ardata)-1; $i++){ 

        //print_r($ardata); 
        echo "</br> </br> ardata "; 
        echo $ardata[$i]; 
        $dataseq = date ('m/d/Y' , strtotime($ardata[$i]));  
        $diffdata = dayDiff(strtotime($today3), strtotime($dataseq)); 
        echo "</br></br> DIFFDATA: "; 
        echo $diffdata; 
        echo " DIFFMIN "; 
        echo $diffmin; 
        if ($diffdata > 0){ // avoiding days before today 
         if ($diffdata < $diffmin){ 
          $diffmin = $diffdata; 
          $nextdata = $ardata[$i]; 
         }else{ 
          if ($diffdata == -1){ 
           echo "error array empty"; 
           $nextdata = $today3; 
          } 
         } 
        }else{ 
         echo "monitorterminato"; 
         $nextdata = -1; 
        } 
       } 

       if ($nextdata != -1){ 

        if ($diffmin == 0){ // giorno di oggi quindi avviso 
         echo "warning WARNING </br> </br>"; 
        }else if ($diffmin > 0){ 

         echo " oggi ". $today3." prossimo:". $nextdata. " </br></br>"; 
         testWarning($today3, $nextdata, $delay); 
        }else{ 
         // caso errato 
         echo " </br> </br> ERRORE nel calcolo non può essere negativo"; 
        }      
       } 

回答

0

這裏是如何計算警告之前離開你的警告日期和天一個例子。

<?php 
header("content-type: text-plain"); 

$end = "09/18/2014"; 
$delay = "- 2 days"; // warning will be on 16th 

$warningDay = date('m-d-Y', strtotime($delay, strtotime($end))); 
echo "Warning is on " . $warningDay . "\n"; 

$today1 = "09/13/2014"; // 3 days before warning 
testWarning($today1, $end, $delay); 

$today2 = "09/16/2014"; // warning day 
testWarning($today2, $end, $delay); 

$today3 = "09/17/2014"; // the day after warning 
testWarning($today3, $end, $delay); 


function testWarning($today, $end, $delay){ 
    $endDate  = strtotime($end); 
    $warningDate = strtotime($delay, $endDate); 
    $todayDate = strtotime($today); 

    echo "today : " . date('m-d-Y', $todayDate) . "->"; 

    if($todayDate == $warningDate){ 
     echo "Warning is today"; 
    }elseif($todayDate < $warningDate){ 
     echo "Warning in " . dayDiff($todayDate, $warningDate) . " days"; 
    }else{ 
     echo "Warning was " . abs(dayDiff($todayDate, $warningDate)) . " days ago"; 
    } 

    echo"\n"; 
} 

// $start and $end are timestamps 
function dayDiff($start, $end){ 
    $timeleft = $end - $start; 
    $daysleft = round((($timeleft/24)/60)/60); 
    return $daysleft; 
} 
+0

對不起,我沒有很好的解釋。當第一個日期結束時,在你的例子中:09/16/2014,today3不必顯示:「Warning was ...」。在今天3,可能會在第二天發出警告,警告根據這段時間:2天是09/18/2014,所以今天3:「今天1天警告」。我必須表明此警告存儲陣列中的所有日期:[07-18/2014年,2014年7月20日,2014年7月22日,2014年7月24日] – user3798245

+0

好我的工作一點點與您的代碼我達到了我的目標。我刪除了延遲變量,因爲沒有用,並且我用你的函數管理了數組序列,我更新瞭解決方案!謝謝! – user3798245

0

可以看出here

$future = strtotime('21 July 2012'); //Future date. 
$timefromdb = //source time 
$timeleft = $future-$timefromdb; 
$daysleft = round((($timeleft/24)/60)/60); 
echo $daysleft; 
+0

這真的是一個很好的解決方案,但是我怎麼能知道7月21日是下一次約會呢?在我的例子中,首先,我必須生成4個不同的「警告」日期並將它們存儲在某個地方;那麼我怎麼能知道未來的日子,我必須選擇做你建議我的事情? – user3798245