简体   繁体   中英

How to display next available delivery date before 4pm in php

I'm using php and mysqli for my project.

My question is about calculating next available shipping dates.

On my checkout page, my website displays the next available shipping date according to the following rules:

I can ship monday - saturday before 4pm.

I have a table "cantship" containing dates in the format d/m/Y that I cannot ship on because im at work.

So, the next available shipping date displayed should be a monday-saturday unless its past 4pm or todays date matches a date in the cantship table.

So, what I need shown:

If the time now is before 4pm and today is a monday-saturday, I can ship today so show todays date (as long as todays date is not in the cantship table)

If the time now is after 4pm, I cannot ship today so the next shipping date should be a mon-sat thats not in the cantship table.

If the calculated shipping date is in the cantship table, I cant ship on that day, so the next shipping date would have to be the next day thats between mon-sat, thats not in the cantship table.

Here is my code so far: Sorry its messy.

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

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

Beginning from a Date with the current time you must modify the dateto the next day if

  1. The time is >= 4 pm or
  2. The day is a Sunday or
  3. The day is in the nonshipping list

The nonshiiping list is given as array:

$noShipDates = [
  '2020-10-28',
  '2020-12-24'
];

The logic can be implemented as a small function.

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

The function returns a string of the form 'yyyy-mm-dd' or false for an error. For test purposes, a date can be specified with the 2nd parameter.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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