Last 7 working days remove duplicate date

Hi,

I am trying to output the last 7 working days below but it keeps showing 2 of the same date.

can any one help work this out please?

include ('config.php');
$i = 0;
$date2 = date('Y-n-j');
$date3 = strtotime ( '-1 day' , strtotime ( $date2 ) ) ;
$date = date ( 'Y-m-j' , $date3 );
$time = time();
while($i<7){
   if(date('D', $time)!='Sun' && date('D', $time)!='Sat'){
      $i++; 
		
		$newdate = array($date);
		foreach ($newdate as $date2)
	  {
		
		$instructions = "SELECT `LairdRef` FROM `tblinspectiondata` WHERE `InstDate` = :date";
        $instruct = $dbh->prepare($instructions);
		$instruct->bindParam(':date', $date);
		$instruct->execute();
		
		
		$count = $instruct->rowCount();
		$newtime = strtotime($date.' UTC');
		$dateInLocal = date("D - j", $newtime);
		echo "['".$dateInLocal."',".$count."],\n";
		
	  }
   }
   $time-= 86400;
  $date = date('Y-n-j', $time);
  
   
   
   
}

output for today is

[‘Mon - 23’,205],
[‘Mon - 23’,205],
[‘Fri - 20’,135],
[‘Thu - 19’,207],
[‘Wed - 18’,220],
[‘Tue - 17’,189],
[‘Mon - 16’,174],

as you can see it’s showing monday twice

Is it because you start the loop checking the day of $time to see if it’s Sunday or Saturday, and $time at this point is set to the current time, but in the pre-amble you’ve set $date to be yesterday? But in the second iteration of the loop, $time has been reduced by a day, and $date set to equal it. So for the first loop $date represents a day earlier than $time, but from the second run through they match?

i ended up fixing via

$i = 0;
$date = date('Y-n-j');
$time = time();
$td = date('D');
while($i<7){
   if(date('D', $time)!='Sun' && date('D', $time)!='Sat' && date('D', $time)!=$td){

so if date = today it’s ignored.

no wait that just now misses the date if it’s todays day.

hmm time to rethink

fixed by changing to a lower case d for the date so it will never show the same date as today :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.