如果指定的时间后再添加另一天发送php

时间:2012-05-09 19:55:33

标签: php date time

我想要完成的事情是我们每周二都会送货。星期四,我想限制订单是否在星期一下午5点之后,然后他们必须等到星期五。如果星期四下午5点之后的订单,他们必须等到星期二。

function freeDelivery($date){
    $holidays = array("05/30/2012","07/04/2012","09/05/2012","11/24/2012","11/25/2012","12/25/2012","12/31/2012","01/01/2013","05/28/2013","07/04/2013","09/03/2013","11/22/2013","11/23/2013","12/25/2013");
    $checkday = strtotime($date);
    // check if it's a holiday
    while(in_array(date("m/d/Y",$checkday), $holidays)) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
    }

    //sun
    if (date("w",$checkday) == 0) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +2 day");
    }
    //mon
    elseif (date("w",$checkday) == 1) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +1 days");
    }
    //tue
    elseif(date("w",$checkday) == 2) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +2 days");
    }
    //wen
    elseif (date("w",$checkday) == 3) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +1 days");
    }
    //thur
    elseif (date("w",$checkday) == 4) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +5 days");
    }
    //fri
    elseif (date("w",$checkday) == 5) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +4 days");
    }
    //sat
    elseif (date("w",$checkday) == 6) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +3 days");
    }

    // make sure it's not another holiday
    while(in_array(date("m/d/Y",$checkday), $holidays)) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
    }
    return $checkday;
}

上述代码用于根据一周中的日期确定发货日期。

谢谢任何帮助,非常感谢。

1 个答案:

答案 0 :(得分:3)

假设你的意思是星期一5p之后是星期四,然后是星期三之后的5p意味着星期二......请尝试以下方法:

修改 最简单的方法是将代码添加回函数中。

function freeDelivery($date){
    $holidays = array("05/30/2012","07/04/2012","09/05/2012","11/24/2012","11/25/2012","12/25/2012","12/31/2012","01/01/2013","05/28/2013","07/04/2013","09/03/2013","11/22/2013","11/23/2013","12/25/2013");
    $checkday = strtotime($date);
    // check if it's a holiday
    while(in_array(date("m/d/Y",$checkday), $holidays)) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
    }

    $thedate = date("m/d/Y",$checkday);
    $dayofweek = date("w",$checkday);
    $dayincrease = array(0 => 2, 1 => 1, 2 => 2, 3 => 1, 4 => 5, 5 => 4, 6 => 3);
    $after5 = (date("G",$checkday) >= 17);

    $increase = "";
    if($after5 && $dayofweek == 1) {
        // monday after 5p = thurs
        $increase = "+3 days";
    } elseif($after5 && $dayofweek == 3) {
        // wednesday after 5p = tues
        $increase = "+6 days";
    } else {
        $increase = "+" . $dayincrease[$dayofweek] . " days";
    }
    $checkday = strtotime($thedate." ".$increase);

    // make sure it's not another holiday
    while(in_array(date("m/d/Y",$checkday), $holidays)) {
        $checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
    }
    return $checkday;
}