0

我想得到明天的约会。但如果明天是Saturdayor Sunday。它将在Monday. 它将跳过周六和周日。此外,如果明天是假期,它将跳过日期并获取下一个日期。我的数据库中有假期日期列表。

我正在尝试这段代码。假设今天是Friday, May 24

$today = "2013-05-24";

$tommorow = date('Y-m-d', strtotime($today . ' + 1 day'));
$valid = check_valid($tommorow);
while (!$valid){
    $tommorow = date('Y-m-d', strtotime($today . ' + 1 day'));
    $valid = check_valid($tommorow);
    if($valid){
        break;
    }
}

function check_valid($date){
    return true;
    $timestamp = strtotime($date);
    $day = date('D', $timestamp);
    if ($day == "Sat" || $day == "Sun"){
        return false;
    }
    $mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
    if (mysql_num_rows($mysql) >= 1){
        return false;
    }
}

我使用while子句是因为我假设holiday date连续两三天出现这个子句。例如,假期是 5 月 27 日和 28 日,那么明天的日期应该是Wednesday.

有任何想法吗?请问你能帮帮我吗?

如果你有另一种方法来实现这一点,我也想知道。

谢谢,

4

6 回答 6

0

You can try this solution, it's based on my working days code that I use in a couple of my projects. It uses Swedish holidays but you can adjust these to the holidays that are observed in your country. Remember that many holidays depend on the current year.

function next_valid_day() {
    $year = date('Y');

    $easter_date = easter_date($year);

    $holidays = array(
                      mktime(0, 0, 0, 1, 1, $year),                                                     // Nyårsdagen
                      mktime(0, 0, 0, 1, 6, $year),                                                     // Trettondedag jul
                      //strtotime('-3 days', $easter_date),                                             // Skärtorsdagen
                      strtotime('-2 days', $easter_date),                                               // Långfredagen
                      strtotime('-1 day', $easter_date),                                                // Påskafton
                      $easter_date,                                                                     // Påskdagen
                      strtotime('+1 day', $easter_date),                                                // Annandag påsk
                      mktime(0, 0, 0, 5, 1, $year),                                                     // 1:a maj
                      strtotime('+39 days', $easter_date),                                              // Kristi himmelsfärdsdag
                      mktime(0, 0, 0, 6, 6, $year),                                                     // Sveriges nationaldag
                      strtotime('-1 day', strtotime('next Saturday', mktime(0, 0, 0, 6, 19, $year))),   // Midsommarafton
                      mktime(0, 0, 0, 12, 24, $year),                                                   // Julafton
                      mktime(0, 0, 0, 12, 25, $year),                                                   // Juldagen
                      mktime(0, 0, 0, 12, 26, $year),                                                   // Annandag jul
                      mktime(0, 0, 0, 12, 31, $year)                                                    // Nyårsafton
                     );

    $valid_date = false;
    $d = 0;

    while ($valid_date === false) {
        $date = strtotime('+' . ++$d  . ' days');

        if (!in_array($date, $holidays) && !(date('N', $date) > 5)) {
            $valid_date = true;
        }
    }

    return date('Y-m-d', $date);
}

var_dump(next_valid_day());
于 2013-05-22T10:11:13.917 回答
0

此函数检查(使用您的逻辑)时间戳是否有效,如果不是则搜索下一个有效时间戳。它将返回可能的第一个有效时间戳的时间戳。

function getNextValidDay($timestamp) {
    $day = date('D', $timestamp);
    if ($day == "Sat" || $day == "Sun"){
        $timestamp += 60*60*24;
        return getNextValidDay($timestamp);
    }
    $mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$timestamp'");
    if (mysql_num_rows($mysql) >= 1){
        $timestamp += 60*60*24;
        return getNextValidDay($timestamp);
    }
    return $timestamp;
}
于 2013-05-22T09:58:00.403 回答
0

问题出在函数 check_valid() 中。您在函数的一开始就返回了 true ,所以它不允许处理剩余的代码,所以它应该是这样的,

function check_valid($date){

    $timestamp = strtotime($date);
    $day = date('D', $timestamp);
    if ($day == "Sat" || $day == "Sun"){
        return false;
    }
    $mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
    if (mysql_num_rows($mysql) >= 1){
       return false;
     } 
    return true;
}
于 2013-05-22T09:58:29.973 回答
0

正如 Syjin 已经说过的,你check_valid()将永远返回true,因为你在return true;函数的开头有一个。

true如果函数之前没有返回,则将其移动到函数的末尾以返回false

于 2013-05-22T09:56:47.803 回答
0

这是代码,正在执行您需要的操作:

它使用 DateTime 类和 do-while 循环:

<?php
function not_valid($date){

      $weekday = $date->format('l');
      if ($weekday == "Saturday" || $weekday == "Sunday"){
            return true;
      }

      $str_date = $date->format('Y-m-d');

      $mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$str_date'");
      if (mysql_num_rows($mysql) >= 1){
            return true;
      }

      return false;
}

$today = new DateTime("2013-05-24");
do{
      //add 1 day
      $today->add(new DateInterval('P1D'));
}while(not_valid($today));

print $today->format('Y-m-d');
?>

一开始是 24.05.2013 - 星期五。然后它加1。25 和 26 是星期六和星期日,因此它会跳过它们并打印 2013-05-27。

于 2013-05-22T10:06:33.553 回答
0

您应该在每个循环中更新/增加“+ 1 天”。将您的代码更改为这样的

$today  = "2013-05-24";
$ii_day = 1;

$tommorow = date('Y-m-d', strtotime($today . ' + '.($ii_day).' day'));
$valid = check_valid($tommorow);
while (!$valid){
    $ii_day++;
    $tommorow = date('Y-m-d', strtotime($today . ' + '.($ii_day).' day'));
    $valid = check_valid($tommorow);
    if($valid){
        break;
    }
}

然后在您的 check_valid 函数上,稍微更改您的代码。

function check_valid($date){
    //return true;
    $timestamp = strtotime($date);
    $day = date('D', $timestamp);
    if ($day == "Sat" || $day == "Sun"){
        return false;
    }
    else {
        $mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
        if (mysql_num_rows($mysql) >= 1){
           return false;
        }
        else 
            return true;
    }
}

希望它有效.. :)

于 2013-05-22T10:06:33.997 回答