Проверьте, что дата включена в период, а затем 10 дней после - PullRequest
1 голос
/ 12 июля 2010

У меня есть переменная $ node-> field_work_start [0] ['view'], которая обозначает дату рождения. Чтобы продолжить, я хочу сделать проверку на факт того, что эта дата включена в следующий период, а затем в 10 дней с текущей даты. Если true, то x = 1, false x = 0. Помогите пожалуйста с кодом PHP.

1 Ответ

0 голосов
/ 12 июля 2010

Похоже, вы получили DATETIME с $ node-> field_work_start [0] ['view'] правильно?Если это так, мы собираемся преобразовать его в метку времени UNIX, чтобы немного упростить процесс.В этом примере мы будем предполагать, что ваша дата хранится в форме M d, Y.

Этот небольшой код должен хорошо работать для вас!

<?php
class Node
{
    // For sake of keeping things similar to your environment
    var $field_work_start = array();
}

/**
 * checkBirthday function
 *
 * The function will check a birth date
 * and see if it is in the range of specified
 * days.
 *
 * @param $birthday
 *   Birth date parameter. Assumed to be in M d, Y form
 * @param $days
 *   The range of days to check where the birthday is
 * @return int
 *   0 if the birthday is _NOT_ within the proper range
 *   1 if the birthday _IS_ within the proper range
 */
function checkBirthday($birthday,$days)
{
    // Parse out the year of the birthday
    $pos      = strpos($birthday,",")+2; // Find the comma which separates the year
    $today    = strtotime(date("M, d")); // Check if day is < Dec. 22. If not, we need to account for next year!

    // Check if the birthday is within the range and has not passed!
    if($today < strtotime("Dec 22")){
        $birthday = substr_replace($birthday,date("Y"),$pos,4); // Replace the year with current year
        if(strtotime($birthday) <= strtotime("+".$days." days") && strtotime($birthday) >= time()){
            return 1;
        }
    } else { // Less than 10 days left in our year.. check for January birthdays!
        if(!strstr($birthday,"December")){
            $birthday = substr_replace($birthday,date("Y")+1,$pos,4); // Replace the year with next year
            $year = date("Y")+1;
        } else { // Still December?
            $birthday = substr_replace($birthday,date("Y"),$pos,4); // Replace the year with current year
            $year = date("Y");
        }
        $day = (date("d")+10)-31; // 10 days from now is...

        if((strtotime($birthday) <= strtotime("January ".$day.", ".$year)) && strtotime($birthday) >= strtotime("December 25, 2010")){
            return 1;
        }
    }
    return 0;
}

$node[]  = new Node;

$node[0]->field_work_start[0]  = "January 1, 1970"; // First birthday is _NOT_ within range
$node[1]->field_work_start[0] = "July 20, 1970"; // Second birthday _IS_ within range

for($i=0;$i<count($node);$i++){
    if(!checkBirthday($node[$i]->field_work_start[0],10)){
        print $node[$i]->field_work_start[0]." is not within the 10 day range.<br /><br />";
    } else {
        print $node[$i]->field_work_start[0]." is within the 10 day range.<br /><br />";
    }
}

unset($node);

?>

Он должен вернуть этот вывод:

January 1, 1970 is not within the 10 day range.

July 20, 1970 is within the 10 day range.

Удачи!

С уважением,Деннис М.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...