我怎样才能管理日期'用于显示下一个日期剩余天数的PHP序列?

时间:2014-07-19 18:40:07

标签: php date datetime strtotime

我的开始日期为2014年7月18日,结束日期为2014年7月24日,期间日期为:每2天一次。 因此,从07/18开始每2天我警告用户必须要做的事情,如果不是正确的一天我会警告用户剩下的剩余天数到下一个警告。 我能怎么做?如果还加上时间和每2小时一段时间?

我想到存储在阵列中的所有日期警告[07-18 / 2014,07-20-2014,07-22-2014,07 / 24/2014]

我的代码如下,但不起作用。也许这不像我使用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/>";                     


    }

我也接受其他建议!

更新解决方案

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

2 个答案:

答案 0 :(得分:0)

here

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

答案 1 :(得分: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;
}