如何管理php中的日期序列以显示下一个日期剩余的天数?

时间:2022-08-25 18:55:51

I have a start date like 07/18/2014, a end date like 07/24/2014, and a period date: every 2 days. So every 2 days starting by 07/18 i warn the user that something have to do, if is not the right day i warn the user about how left days remaining to the next warning. How can i do? And if add also time and a period of every 2 hours?

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

I thought about store first all the dates warning in an array [07-18/2014, 07-20-2014, 07-22-2014, 07/24/2014]

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

My code is the following but it does not work. Maybe it is not correct like I use strtotime

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


    }

I accept also other suggestions!

我也接受其他建议!

UPDATE SOLUTION

                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 个解决方案

#1


0  

Here is an example on how to calculate your warning dates and days left before warning.

以下是有关如何计算警告日期和警告前几天的示例。

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

#2


0  

As seen here

如此处所见

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

#1


0  

Here is an example on how to calculate your warning dates and days left before warning.

以下是有关如何计算警告日期和警告前几天的示例。

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

#2


0  

As seen here

如此处所见

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