如何在php中显示下午4点之前的下一个可用交货日期

如何在php中显示下午4点之前的下一个可用交货日期,php,date,e-commerce,shipping,Php,Date,E Commerce,Shipping,我在我的项目中使用php和mysqli 我的问题是关于计算下一个可用的装运日期 在我的结帐页面上,我的网站根据以下规则显示下一个可用的发货日期: 我可以在周一到周六下午4点之前发货 我有一个表格“cantship”,其中包含的日期格式为d/m/Y,我无法发送,因为我在工作 因此,显示的下一个可用发货日期应为周一至周六,除非其过去的下午4点或今天的日期与cantship表中的日期匹配 所以,我需要展示的是: 如果现在的时间是下午4点之前,今天是星期一到星期六,我可以今天发货,所以显示今天的日期(只

我在我的项目中使用php和mysqli

我的问题是关于计算下一个可用的装运日期

在我的结帐页面上,我的网站根据以下规则显示下一个可用的发货日期:

我可以在周一到周六下午4点之前发货

我有一个表格“cantship”,其中包含的日期格式为d/m/Y,我无法发送,因为我在工作

因此,显示的下一个可用发货日期应为周一至周六,除非其过去的下午4点或今天的日期与cantship表中的日期匹配

所以,我需要展示的是:

如果现在的时间是下午4点之前,今天是星期一到星期六,我可以今天发货,所以显示今天的日期(只要今天的日期不在cantship表中)

如果现在的时间是下午4点以后,我今天不能发货,所以下一个发货日期应该是周一到周六,这不在cantship表中

如果计算的发货日期在cantship表中,我不能在那天发货,因此下一个发货日期必须是mon-sat之间的第二天,这不在cantship表中

这是我到目前为止的代码:对不起,太乱了

<?php
function nextshipdate ($db)
{
// ========================================================================
// find next available date for despatch
// ========================================================================

$i = 0; 
 
$cutoff = strtotime('today 16:00');
$today1 = strtotime('today');

if ($cutoff >strtotime($today1 ))
  {
    echo "<p>its after 4pm!<p>";
    $i = 1;
  }


$tmpDate =date('d-m-Y H:i:s');
$nextBusinessDay = date('d-m-Y ', strtotime($tmpDate . ' +' . $i . ' Weekday'));
$nextavail= date('d-m-Y H:i:s', strtotime($tmpDate . ' +' . $i . ' Weekday'));
 
$dontuse = array();
$getbiz   = $db->get_results("SELECT * from   cantship      ");
$nowcheck = new DateTime();


// =======================================================
// remove past dates from cantship table
// =======================================================
foreach ($getbiz as $inpast)
{
  if (strtotime($inpast->csdate)<= time()) 
    {
      //echo  $inpast->csdate." has passed!<p>";
      $removeold = $db->query("DELETE from  cantship where   csdate='". $inpast->csdate."'" );
    }
  
}


// =======================================================
// create array of unavailable shipping dates
// =======================================================

if ($getbiz)
  {
    foreach ($getbiz as $na)
      {
 $dontuse[]=$na->csdate;
      }

  }
 

while (in_array($nextBusinessDay, $dontuse)) 
{
    $i++;
    $nextBusinessDay = date('d-m-Y', strtotime($tmpDate . ' +' . $i . ' Weekday'));
}


if(!empty($dontuse)) 
{
  $nbd=$nextBusinessDay;
}
else
{
$nbd=' please contact us.';
}


return $nbd;
}

// ==========================================================================================

从当前时间的日期开始,如果

  • 时间>=下午4点或
  • 这一天是星期天或星期天
  • 这一天在非发货清单上
  • 非单管列表以数组形式给出:

    $noShipDates = [
      '2020-10-28',
      '2020-12-24'
    ];
    
    该逻辑可以实现为一个小功能

    function nextShipDate(array $noShipDates, $curTime = "now"){
      $date = date_create($curTime);
      $max = 1000;  //protection endless loop
      while($max--){
        if($date->format('H') >= 16 
          OR $date->format('w') == 0
          OR in_array($date->format('Y-m-d'),$noShipDates)
        ) {
            $date->modify('next Day 00:00');
          }
        else {
            return $date->format('Y-m-d');
          }
      }
      return false;
    }
    
    函数返回格式为“yyyy-mm-dd”的字符串,或返回错误的false。出于测试目的,可以使用第二个参数指定日期

    echo nextShipDate($noShipDates, '2020-10-27 16:01');
    //2020-10-29