2

配列内の2つの日付の間のすべての日付を返す関数がありますが、その配列内の日曜日を除外する必要があります。

public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 
    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);
    while( $current <= $last ) { 
        $dates[] = date($format, $current);
        $current = strtotime($step, $current);
    }
    return $dates;
}

日曜日を除外した後、いくつかの日付を格納するテーブルがあります。それらの日付も配列から除外する必要があります。

たとえば、日付範囲を01-05-2012(DD-MM-YYYY)から10-05-2012として入力すると、06-05-2012は日曜日になり、日付は01-05-2012&08-05-になります。 2012年は私が上で述べたテーブルになります、最終的な出力は次のようになるはずです、

02-05-2012
03-05-2012
04-05-2012
05-05-2012
07-05-2012
09-05-2012
10-05-2012

PHPでこれを行う方法は?いくつか試してみましたが、正しい方法が見つかりませんでした。

4

3 に答える 3

2

日曜日の部分の場合:

public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 
    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);
    while( $current <= $last ) { 
        if (date("D", $current) != "Sun")
            $dates[] = date($format, $current);
        $current = strtotime($step, $current);
    }
    return $dates;
}

休日の部分:

まず、日付をある種の配列にロードしてから、各日付の配列をループして、それらが一致するかどうかを確認する必要があります。

于 2012-05-15T07:04:31.977 に答える
0

私は私の質問に対する答えを見つけました、私を助けてくれた人々に感謝します。

public function dateRange($first, $last, $step = '+1 day', $format = 'd/m/Y' ) { 
    $dates = array();
    $current = strtotime($first);
    $last = strtotime($last);
    while( $current <= $last ) {
        $sql = "SELECT * FROM ost_holidays where holiday_date='".date('Y-m-d', $current)."' LIMIT 1";
        $sql = db_query($sql);
        $sql = db_fetch_array($sql);
        if($sql['holiday_date'] != date('Y-m-d',$current))
            if (date('w', $current) != 0)
            $dates[] = date($format, $current);
            $current = strtotime($step, $current);
    }
    return $dates;
}

上記のコードは、指定された範囲の休日と日曜日を削除するためのものです。

于 2012-05-16T04:44:32.910 に答える
-1

私はこれと同じ上記のメソッドをJqueryで実行しました

//Convert dates into desired formatt
 function convertDates(str) {
     var date = new Date(str),
         mnth = ("0" + (date.getMonth() + 1)).slice(-2),
         day = ("0" + date.getDate()).slice(-2);
     return [date.getFullYear(), mnth, day].join("-");
 }

 // Returns an array of dates between the two dates
 var getDates = function(startDate, endDate, holidays) {
     var dates = [],
         currentDate = startDate,
         addDays = function(days) {
             var date = new Date(this.valueOf());
             date.setDate(date.getDate() + days);
             return date;
         };
     while (currentDate <= endDate) {
         dates.push(currentDate);
         currentDate = addDays.call(currentDate, 1);
     }
     return dates;
 };
 //Indise Some Function
 var datesTemp = [];
 var dates = getDates(new Date(prodDet.details.date1), new Date(prodDet.details.date2));
 dates.forEach(function(date) {
     if (date.getDay() != 0) {
         datesTemp.push(convertDates(date));
     }
 });
 datesTemp.forEach(function(date) {
     for (var j = 0; j < prodDet.holidays.length; j++) {
         if ((prodDet.holidays[j] != date)) {
             ideal.idates.push(date);
         }
     }
 });
 console.log(ideal.idates);
 //Function Ends Here
于 2017-08-27T15:14:09.020 に答える