Home > Back-end >  I want to store array of alternate days of the week in correct way [PHP]
I want to store array of alternate days of the week in correct way [PHP]

Time:07-07

The code I have right now stores alternate days of the week in an array format. The logic runs fine for daily deliveries, but breaks for next week if alternate day delivery is chosen from the app side. Suppose, One week = 7 days. So $array gets a response from app [0,2,4,6] and stores Monday, Wednesday, Friday and Sunday, for next set of dates, array again starts from 0 and stores Monday and so on.

What I want is, for next set of week it should continue the alternate day format, and start from Tuesday.

Here is the code. Please suggest me a correction. I have commented what the app sends from app side.

$sdate = explode('-',$ProductData[$i]['startdate']); //customer chooses the date
$sdates = $sdate[2].'-'.$sdate[1].'-'.$sdate[0];
$array = explode(',',$ProductData[$i]['select_days']); //customer chooses the days he wants delivery for on week basis [0 for monday - 6 for sunday]
$sday = $ProductData[$i]['select_days'];
$delivery = $ProductData[$i]['total_deliveries']; //customer chooses the number of deliveries it needs. e.g. 5, 10, 15 etc.
$tslot = $ProductData[$i]['tslot']; //timeslot 5:00 AM to 7:00 AM
$dates = array();
$pol = array();
$date = new DateTime($sdates);
for($ip=0;$ip<count($array);$ip  )
{
    $pol[] = $array[$ip] 1; 
}

while (count($dates)< $delivery)
{
    $date->add(new DateInterval('P1D'));
   if (in_array($date->format('N'),$pol)) 
    $dates[]=$date->format('Y-m-d');
    
}
$rdate = implode(',',$dates); // dates on which customer will get delivery

CodePudding user response:

$startDate = new DateTime('2022-07-02');
$countOfWeeks = 3;
$stepOfDays = 2;

$dates = [];
$days = [];
for ($i = 0; $i <= $countOfWeeks * 7; $i = $i   $stepOfDays) {
  $date = $startDate->add(DateInterval::createFromDateString('2 days'));
  $dates[] = $date->format('y-m-d');
  $days[] = $date->format('D');
}

$resultDates = implode(', ', $dates);
$resultDays = implode(', ', $days);

echo $resultDates . "\n";
echo $resultDays . "\n";

Output:

22-07-04, 22-07-06, 22-07-08, 22-07-10, 22-07-12, 22-07-14, 22-07-16, 22-07-18, 22-07-20, 22-07-22, 22-07-24
Mon, Wed, Fri, Sun, Tue, Thu, Sat, Mon, Wed, Fri, Sun

CodePudding user response:

We Can Do Something Like This ->`

<?php

$startDate = new DateTime('2022-07-02');

$delivery = 15;
$i = 0;
$Aflag = 2;/*(2 for Alternate day, 1 For All days)*/

$dates = [];
$days = [];

while ($i < $delivery) {

    if ((($i  % 6) % $Aflag) == 0) {

        $date = $startDate->add(DateInterval::createFromDateString('1 days'));
        $dates[] = $date->format('y-m-d');
        $days[] = $date->format('D');
    } else {
        $date = $startDate->add(DateInterval::createFromDateString('1 days'));
    }
    $i  = 1;
}

$resultDates = implode(', ', $dates);
$resultDays = implode(', ', $days);

echo $resultDates . "\n";
echo $resultDays . "\n";

`

OutPut:22-07-03, 22-07-05, 22-07-07, 22-07-09, 22-07-11, 22-07-13, 22-07-15, 22-07-17 Sun, Tue, Thu, Sat, Mon, Wed, Fri, Sun

  • Related