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

问题描述

我正在为项目使用php和mysqli。

我的问题是关于计算下一个可用的发货日期。

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

我可以在星期一至星期六下午4点之前发货。

我有一个表格“ cantship”,其中包含日期(格式为d / m / Y)的日期,由于我在工作,因此无法发货。

因此,显示的下一个可用的运送日期应该是周一至周六,除非其过去的下午4点或今天的日期与活动表中的日期匹配。

所以,我需要显示的内容:

如果现在的时间是下午4点之前,而今天是星期一至星期六,那么我今天就可以发货,因此请显示今天的日期(只要今天的日期不在活动表中)

如果现在的时间是下午4点以后,那么我今天就不能发货,所以下一个发货日期应该是周一到周六,而不是活动表中的日期。

如果计算出的运输日期在可兑换表中,则我无法在该天发货,因此下一个运输日期必须是星期一至六之间的第二天,而不是可兑换表中的第二天。

到目前为止,这是我的代码:对不起,它很混乱。

<?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;
}

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

解决方法

从具有当前时间的日期开始,您必须将日期修改为第二天

  1. 时间是> = 4 pm或
  2. 当天是星期天或
  3. 这一天在非发货清单中

非运输清单以数组形式给出:

$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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...