Jump to content
Sign in to follow this  
Balderick

how to use this function for multiple records instead of 1

Recommended Posts

I found an online function called GetDays and I slightly modified it to calculate the number of days between a past date and the current time.

 

The function works fine for 1 input, but I have a table full of records and want to use the function to get the total days for each record separatedly.

     <?php
  
     //
    function GetDays($sStartDate, $sEndDate){  
    // Firstly, format the provided dates.  
     // This function works best with YYYY-MM-DD  
    // but other date formats will work thanks  
    // to strtotime().  
    $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));  
    $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));  
  
    // Start the variable off with the start date  
    $aDays[] = $sStartDate;  
  
     // Set a 'temp' variable, sCurrentDate, with  
     // the start date - before beginning the loop  
     $sCurrentDate = $sStartDate;  
  
     // While the current date is less than the end date  
     while($sCurrentDate < $sEndDate){  
     // Add a day to the current date  
      $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));  
  
     // Add this new day to the aDays array  
	 global $aDays; // deze global heb ik zelf toegevoegd en MOET hier staan, iig niet buiten deze conditie
	 global $tot_days;
     $aDays[] = $sCurrentDate;
	
     }  
  
     
     $tot_days = count($aDays);
   
     $tot_days = $tot_days +1;
     return $tot_days;  
     }  



    // function ends

    // 

     // here the records are fetched from the database table

    while($row = $result->fetch_assoc()){
		  echo "<center>";
   	$array = implode(" / " , $row);
	   
	
	   $sStartDate = $row['created_on'];
	$sEndDate = date("Y-m-d");
     GetDays ($sStartDate, $sEndDate);

  
	echo "</center>";
    
     ?>
    <center>
    <form action="#" method="post" > 
    
    <input type="text" name="test" value="<?php echo $array . ' / ' . $sEndDate . ' / ' . $tot_days ; ?>" size="60"/>
    </center>	

    <?php 

    }

     // rest of the code to complete the form -->
    ?>

Share this post


Link to post
Share on other sites

I cant find any solution, but I made a more simplified set-up for the function. Not yet with a loop, but the output is wrong in the same way.

 <?php 


		function GetDays($sStartDate, $sEndDate){  
		  // Firstly, format the provided dates.  
		  // This function works best with YYYY-MM-DD  
		  // but other date formats will work thanks  
		  // to strtotime().  
		  $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));  
		  $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));  
		  
		  // Start the variable off with the start date  
		  $aDays[] = $sStartDate;  
		  
		  // Set a 'temp' variable, sCurrentDate, with  
		  // the start date - before beginning the loop  
		  $sCurrentDate = $sStartDate;  
		  
		  // While the current date is less than the end date  
		  while($sCurrentDate < $sEndDate){  
			// Add a day to the current date  
			$sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));  
		  
			// Add this new day to the aDays array  
			 global $aDays; // deze global heb ik zelf toegevoegd en MOET hier staan, iig niet buiten deze conditie
			 global $tot_days;
			$aDays[] = $sCurrentDate;
			
		  }  
		  
		  // Once the loop has finished, return the  
		  // array of days. 
		  $tot_days = count($aDays);
		  //return $aDays;
		  $tot_days = $tot_days +1;
		return $tot_days;  
		}  


		 $sStartDate ='';
		$sStartDate = '2016-10-1';
		 
		$sEndDate =date("Y-m-d");


		GetDays ($sStartDate, $sEndDate);

					

						var_dump($tot_days);

		echo '<br> ' . $tot_days . ' is the number of days that ' . $sStartDate . ' and ' . $sEndDate . ' are separated ! ' ;


		$sStartDate ='';
		$sStartDate = '2016-9-1';
		 
		$sEndDate =date("Y-m-d");


		GetDays ($sStartDate, $sEndDate);

					

						var_dump($tot_days);

		echo '<br> ' . $tot_days . ' is the number of days that ' . $sStartDate . ' and ' . $sEndDate . ' are separated ! ' ;

		$sStartDate ='';

		$sStartDate = '2016-9-24';
		var_dump($sStartDate); 
		$sEndDate =date("Y-m-d");


		GetDays ($sStartDate, $sEndDate);

					

						var_dump($tot_days);

		echo '<br> ' . $tot_days . ' is the number of days that ' . $sStartDate . ' and ' . $sEndDate . ' are separated ! ' ;
    ?>

Though, the difference between output 3 and 2 is eaxctly the number of days I'm looking for.

 

is there another, better and faster way to solve this?

 

Share this post


Link to post
Share on other sites

Adding global variables to a function like that is not a good idea. You can return an array if you need to return multiple values. I would start over without using global variables. If you need to do calculations for multiple dates at one time then pass an array of dates, or else just call the function once for each date pair.

Share this post


Link to post
Share on other sites

I finally fixed it, but I used global.

 

Is there a difference in using global and using an array as output? In general, is it bad to use global to augment the scope of a variable output or only in specific circumstances?

 

Solution:

	<?php


	function count_all_days($input){
			global $input;
			global $out;
		$pres = new DateTime();
		 $date_set = date_create($input);
		$pres->format('Y-m-d');
			$countday = date_diff($date_set, $pres);

			$out = $countday->format("%R%a days");
			$out = $out+2; // the first and last day are not counted
		return $out;
	} 


	?>

The output, which is not an array, is suitable for usage inside my html lay out.

Edited by Balderick

Share this post


Link to post
Share on other sites

Using global variables in functions like that can cause side effects that can lead to bugs later on where the cause isn't obvious. The list of parameters is what you use for input for a function, and you return whatever needs to be returned as output. You can also pass variables by reference if you want to use them for both input and output. If you need to return multiple values then you would return an array.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×