开发者

end date validation

I would like validate the current date with question posted date if date difference is greater then 1 day then it needs to show in over due question also this will not consider the array of dates which are already i having in my array.

开发者_如何学CLike if a question posted on 20-July-11.

exemption date array {'21-July-11', ... etc} current date is 22-July-11

then the output needs to be shown like question is waiting for 1 day instead of 2 days.

can any one help on it.


That is some example code I have created for you to calculate the due date. When you submit a question, calculate the due date that way and store it in the database with entry for that question...

<?php

$daysDue = 2;
$exDatesArray = array("2011-07-21", "2011-07-23"); //array with all your holiday dates.
$question_1_Date = "2011-07-21";
$question_2_Date = "2011-07-18";
$question_3_Date = "2011-07-20";

echo "Holidays on: ";
foreach( $exDatesArray AS $exdate )
{
    echo $exdate . ", ";
}
echo "<br/><br/>";
echo "submit date -> due date<br/>";
echo $question_1_Date . " -> " . calculateDueDate( $question_1_Date, $exDatesArray, $daysDue ) . "<br/>";
echo $question_2_Date . " -> " . calculateDueDate( $question_2_Date, $exDatesArray, $daysDue ) . "<br/>";
echo $question_3_Date . " -> " . calculateDueDate( $question_3_Date, $exDatesArray, $daysDue );

function calculateDueDate( $date, $exDates, $daysDue )
{
    //start with day 1
    $count = 1;

    //now we loop from day one to due days
    while( $count <= $daysDue )
    {
        //add one day to start date
        $date = add_date( $date, 1 );
        //check if that new date is a holiday
        if( !in_array($date , $exDates) )
        {
            //only if it is not a holiday we increase counter, otherwise we dont count that day towards due period
            $count++;
        }
    }

    //return calculated due date
    return $date;
}

function add_date($date,$days)
{ 
    $cd = strtotime($date); 
    return date('Y-m-d', mktime(0,0,0,date('m',$cd),date('d',$cd) + $days, date('Y',$cd))); 
}
?>

Output:

Holidays on: 2011-07-21, 2011-07-23, 

submit date -> due date
2011-07-21 -> 2011-07-24
2011-07-18 -> 2011-07-20
2011-07-20 -> 2011-07-24


//convert date to unixtime (seconds)
$question_sec = strtotime($question);

//subtract
$diff = time() - $question_sec;
$days = $diff / 60*60*24;
echo "$days old";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜