Calulating days for delivery

Thanks! I’ll have a play around with it as soon as I get some spare time :wink:

Okay, for testing, put in some holidays like saturday or monday, some soon so you can see if it works.
I am quite busy today, so I will leave the testing up to you…

I thought I’d sorted this but alas, no :frowning:

<?php

	class BoDelivery {

        public function EstimatedDaysWithHoliday($off, $days, $saturday) {

            require_once FILE_ROOT . '/data_objects/do_settings.php';
            $doSettings = new DoSettings();
            
            // Holidays that happen every year in MM-DD format. 
            $holidays_every_year=($doSettings->GetSetting(51));
            $date_array=  explode(',',$holidays_every_year);
            $holidays_every_year=  array();
            foreach ($date_array as $value) {
                trim($value);
                if (strtotime($value)=== false ){
                    //bad date
                }else{
                    $holidays_every_year[] = date('m-d',strtotime($value));
                }
            };

            // Holidays that happen only ONCE in YYYY-MM-DD format.
            $holidays_by_year=($doSettings->GetSetting(50));
            $date_array=  explode(',',$holidays_by_year);
            $holidays_by_year=  array();
            foreach ($date_array as $value) {
                trim($value);
                if (strtotime($value)=== false ){
                    //bad date
                }else{
                    $holidays_by_year[] = date('Y-m-d',strtotime($value));
                }
            };

            $today = date("N"); // Weekday - number 1-7
            $now = strtotime("now"); // Unix
            
            $off_array = explode(":", str_replace(".", ":", $off));
            
            $off_unix = mktime($off_array[0], $off_array[1], "00", date("n"), date("j"), date("Y"));

            $sending_days_from_now = 0;
            if ($now > $off_unix) {
                $sending_days_from_now++;
            }
            $sending_day = $today + $sending_days_from_now;
            switch ($sending_day) {
                case 6:
                    $sending_days_from_now++;
                    $sending_days_from_now++;
                    break;
                case 7:
                    $sending_days_from_now++;
                    break;
            }
            
            $count5WD = 0;
            $temp = strtotime("now"); // Todays date
            if($sending_days_from_now > 0) {
                $temp = strtotime('+'.$sending_days_from_now.' weekday', $temp);
            }
            
            while($count5WD<$days){
                $currentDay = date('N', $temp);
                if($currentDay == 5 && $saturday) {
                    $next1WD = strtotime('+1 day', $temp);
                }
                else {
                    $next1WD = strtotime('+1 weekday', $temp);
                }
                $MMDD=date("m-d", $next1WD);
                $YYYYMMDD=date("Y-m-d", $next1WD);
                
                if(!(in_array($MMDD, $holidays_every_year)) && !(in_array($YYYYMMDD, $holidays_by_year))) {
                    $count5WD++;
                }
                $temp = $next1WD;
            };
            
            $next5WD = date("D jS F", $temp);
            
            return $next5WD; // + working days, extra day if holiday day is in array
        }
				
	}
	
?> 

With the above code it tells me standard delivery will be on Wednesday 26th it should be Tuesday 25th.

It all looks correct to me, date is N so a Saturday (today) is 6 & Sunday is 7, the code says add 2 days if a Saturday & 1 if a Sunday but it’s not doing that.

Any ideas?

Well, you switched back to the old code using mktime, === and odd OFF case switches.
Did you play with the routine I created for you? I mean, is all that questionable code needed?

If you must keep all the old code, you need to debug it. To do that, there are two ways. You can test it line
by line and see the results of every line. Or you could have the data out of each line be sent to a text file
and then see what everything is done. You can write to a text file using a line like this:
file_put_contents(“temp_file.txt”, some-variable…, FILE_APPEND);
You can put that line in several places and have it show you details. For instance, you could display the
value of $now or $off_unix or any others. I add in crlf’s after each line so the output is on separate lines.

Keep at it and let us know how it goes…

Hi, I think it’s down to the weekday number (which would make sense), if I echo the variable it’s outputting multiple times for some reason. eg,

echo $today;

Gives

77 77 77 77 77 77 77 77 77

Where’s as it should (or needs to be!) just one 7??

I made a simple file to check :-

<?php $today = date("N"); // Weekday - number 1-7 echo $today; ?>

Outputs 7 as expected.

Well, try echo $today . “
”; so it is not stacking up the outputs.
I bet then it says
7
7

7
7
etc…

I’ll try tomorrow (long day again!)

If it outputs as you think wouldn’t that still cause issues? I don’t understand why it’s doing it? Looping through somewhere for some reason??

I think it repeated 18 times in total. There are 9 delivery options available.

Well, it depends on where you placed the echo. The last post showed us a “class”. Therefore, it depends on where it is called from and where you place the echo for the $today. If you placed it right after it was created, it should only display one time. Well, once each time the class is called. If the class is called from an AJAX call or other routines on the page, it might be processing it more than once. But, again, it depends where you place the echo.

I placed it right after the $today = date(“N”); // Weekday - number 1-7.

First, you do not use semicolon’s after ending braces… You use them after lines of code, not the
grouping of code…

Sponsor our Newsletter | Privacy Policy | Terms of Service