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

jjjwad0x  于 2023-01-16  发布在  PHP
关注(0)|答案(1)|浏览(111)

我的项目使用php和mysqli。
我的问题是关于计算下一个可用的装运日期。
在我的结帐页面上,我的网站根据以下规则显示下一个可用的发货日期:
我可以在星期一到星期六下午4点前发货。
我有一个表“cantship”包含日期的格式d/m/Y,我不能船舶上,因为我在工作。
因此,下一个可用的航运日期显示应该是星期一至星期六,除非其过去的下午4时或今天的日期匹配的日期在cantship表。
所以,我需要展示的是:
如果现在的时间是在下午4点之前,今天是星期一至星期六,我可以今天船,所以显示今天的日期(只要今天的日期是不是在cantship表)
如果现在的时间是下午4时后,我不能船舶今天,所以下一个航运日期应该是一个星期一,这不是在坎特表。
如果计算出的航运日期是在cantship表,我不能船舶在这一天,所以下一个航运日期将不得不是第二天之间的星期一至星期六,这不是在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;
}

// ==========================================================================================
jpfvwuh4

jpfvwuh41#

从当前时间的日期开始,您必须将日期修改为下一天,如果
1.时间〉= 4 pm或
1.当天是星期日或
1.该日在不发货列表中
nonshiping列表以数组形式给出:

$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

相关问题