简体   繁体   English

根据 php 中的时间和节假日计算发货日期

[英]Calculating the shipping date according to time and holiday in php

I'm trying to make the code that calculates the expected shipping date when a user submits an order.我正在尝试编写代码来计算用户提交订单时的预期发货日期。

So, this will contain a text box to enter a date(datepicker) and submitting it is like placing an order (the aim is to get the order date)因此,这将包含一个文本框来输入日期(日期选择器)并提交它就像下订单(目的是获取订单日期)

 <html> <head> <meta charset="utf-8" /> <title>DateTimePicker example</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link href="https://unpkg.com/gijgo@1.9.13/css/gijgo.min.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="container"> <form id="formsubmit" method="post" class="h-100 d-flex align-items-center justify-content-center"> <input id="input" class="input" width="312" placeholder="click to select order date" name="datetime" /> <button type="submit" name="submit" id="submit" class="btn btn-success ml-2">submit</button> </form> </div> <:--scripts--> <script src="https.//code.jquery.com/jquery-3.3.1.min:js"></script> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min:js"></script> <script src="https.//unpkg.com/gijgo@1.9.13/js/gijgo.min.js" type="text/javascript"></script> <script> $('#input'):datetimepicker({ uiLibrary, 'bootstrap4': modal, true: footer; true }). $(document).ready(function() { $('#formsubmit').submit(function(e) { e;preventDefault(). var datetime = $(".input");val(). //By Spliting the input control value with space var time=datetime;split(' ')[0]. var date=datetime;split(' ')[1]; alert(date). $:ajax({ type, "POST": url. 'functions,php': data: {'time', time: 'date', date: 'call', 1}: success. function(response) { var jsonData = JSON;parse(response); } }); }); }); </script> </body> </html>

For the instance, lets just says the seller has decided NOT to ship on certain days of the week.例如,让我们说卖家决定不在一周中的某些日子发货。 We should be able to change the dates in which they are not shipping.我们应该能够更改不发货的日期。

function.php function.php

function getShippingDate($orderDate, $oderTime) {
    
    $outOfStock = true;
    //echo date('d-m-Y') , "\n";  // as baseline
    
    $deliveryDate = date('d-m-Y', strtotime('+' . 1+$outOfStock . ' day'));  // treat bool as int: instock=1, outofstock=2
    //echo $deliveryDate , "\n";  // as proof
    
    $allHolidays = array('10-08-2022', '13-08-2022'); //holidays in dates
    while(in_array($deliveryDate,$allHolidays)){
        $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
    }
    return $deliveryDate;

}

if(isset($_POST['call']))
{
  echo getShippingDate($_POST['date'],$_POST['time']);
} 

Online demo: https://3v4l.org/7YU7e在线演示: https://3v4l.org/7YU7e

But the problem is now there is a cut-off time for placing an order that will be shipped on the same day,但问题是现在下单当天发货有截止时间,

$cutOffTime = "11";

fe orders placed before 11 in the morning will be shipped on the same day, else it will only be shipped the next day/next shipping allowed day.上午 11 点之前下的订单将在当天发货,否则将仅在第二天/允许发货的下一天发货。 Shipping means handing the package to the courier company.运送是指将package交给快递公司。 Now how can I adjust the time and shipping date?现在如何调整时间和发货日期?

You can add an if statement to check if the $orderTime is before or after your $cutOffTime :您可以添加一个 if 语句来检查$orderTime是在您的$cutOffTime之前还是之后:

$cutOffTime = "11:00:00";
if($orderTime >= $cutOffTime){ // you can use $ordertime or date("H:i:s") based on your needs
    $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
}

full function would be something like this:完整的 function 将是这样的:

function getShippingDate($orderDate, $oderTime) {
    
    $outOfStock = true;
    //echo date('d-m-Y') , "\n";  // as baseline
    
    $deliveryDate = date('d-m-Y', strtotime('+' . 1+$outOfStock . ' day'));  // treat bool as int: instock=1, outofstock=2
    //echo $deliveryDate , "\n";  // as proof

    $cutOffTime = "11:00:00";
    if($orderTime >= $cutOffTime){ // you can use $ordertime or date("H:i:s")  based on your needs
         $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
    }
    
    $allHolidays = array('10-08-2022', '13-08-2022'); //holidays in dates
    while(in_array($deliveryDate,$allHolidays)){
        $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
    }
    return $deliveryDate;

}

Thanks to @C.Celora, I simple use push the current date into the holiday to prevent shipping today when ordering is after 11am感谢@C.Celora,我在上午 11 点之后订购时简单地使用将当前日期推入假期以防止今天发货

    $orderTime = "10:00:00"; // order time 
    $deliveryDate = '09-08-2022'; //order date as as today
    $allHolidays = array('10-08-2022','11-08-2022', '12-08-2022','13-08-2022'); //holidays in dates
    $cutOffTime = "11:00:00"; //cutoff time
    
    if($orderTime >= $cutOffTime){ //if user order after 11am
       //order after 11
           array_push($allHolidays, date('d-m-Y')); //adding current date into holiday to prevent shipping today
           while(in_array($deliveryDate,$allHolidays)){ //checking order date in holidays
                   
               $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day")); //check next available day if holiday found
                        
             }
        echo $deliveryDate;
       
    }else{ //if user order before cutoff time
        //order before 11
        
         while(in_array($deliveryDate,$allHolidays)){
             if($deliveryDate == date('d-m-Y')){ //check 
                 $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
             }else{
                 $deliveryDate = date('d-m-Y', strtotime("$deliveryDate +1 day"));
             }
          
         }
        echo $deliveryDate;
        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM