Page 1 of 1

Function BUG ??

Posted: Tue Feb 27, 2007 11:54 am
by ddragas
Hi all

I've made this function for "counting" how many days are in selected period on x price.
Lets say that an renting object has "x" seasons and every season has it's own price.
User selects some other Y period for renting (spending hollidays in rented object)

Now, total period should be calculated from this function, but it is not calculating right.

Here is function

Code: Select all

<?

ponuda("03/01/2007", "03/12/2007", "2");

function ponuda($start_date, $end_date, $objekt)
	{

		$broj_dana_u_periodu = array();
		
		$start_date_reserv = strtotime($start_date);
		$end_date_reserv = strtotime($end_date);
		$sezone = array(	'03/21/2007-03/31/2007','03/11/2007-03/20/2007', '03/01/2007-03/10/2007', '02/01/2007-02/09/2007', '02/10/2007-02/28/2007');
		$cijene = array(57,20,15,35,48);
		
			for($i = 0; $i < count($sezone); $i++)
				{
					$temp = explode("-", $sezone[$i]);
					$temp[0] = round(strtotime($temp[0]) / 86400);
					$temp[1] = round(strtotime($temp[1]) / 86400);
					$broj_dana_u_periodu[] = $temp[0] . "-" . $temp[1];
				}

		$dif_days_reserv = round(($end_date_reserv - $start_date_reserv) / 86400);
		
		if($dif_days_reserv > 0)
			{
				$poc_dan = date("d" , $start_date_reserv);
				$poc_mj = date("m" , $start_date_reserv);
				$poc_datum = $poc_mj . "/" . $poc_dan . "/" . date("Y" , $start_date_reserv);
				$poc_datum = round(strtotime($poc_datum) / 86400);

				$kraj_dan = date("d" , $end_date_reserv);
				$kraj_mj = date("m" , $end_date_reserv);
				$kraj_datum = $kraj_mj . "/" . $kraj_dan . "/" . date("Y" , $end_date_reserv);
				$kraj_datum = round(strtotime($kraj_datum) / 86400)-1;
				
				
				
					for($i = $poc_datum; $i <= $kraj_datum; $i++)
						{
							for($j = 0; $j < count($broj_dana_u_periodu); $j++)
								{
									$sez = explode("-", $broj_dana_u_periodu[$j]);
										if(($i >= $sez[0]) && ($i <= $sez[1]))
											{
												$brojanje_cijena[$j]++;
											}
								}
						}
			}
		for($i = 0; $i < count($brojanje_cijena); $i++)
			{
				if(($brojanje_cijena[$i] != 0) && ($cijene[$i] != 0))
					{
						$ukupno += $brojanje_cijena[$i] * $cijene[$i];
						$rezultat .= $brojanje_cijena[$i] . " days at " . $cijene[$i] . " = " . $brojanje_cijena[$i] * $cijene[$i] . " €<br>";
					}
			}
			
			echo $rezultat;
			echo $ukupno;
									


	}

?>
Based on this values of periods and seasons the result should be

1 day(s) at 20€ = 20
10 day(s) at 15€ = 150
Total = 170€

and I'm getting result on

1 day(s) at 20€ = 20
Total = 20€

If somebody can help me find this bug because I'll get mad with it.
Spend last 2 days on this with no result!!

regards
ddragas

Posted: Tue Feb 27, 2007 1:06 pm
by Begby
You might want to try maybe commenting your function to start with, I don't think anybody here is going to try and figure out what its supposed to be doing.

Posted: Tue Feb 27, 2007 3:23 pm
by ddragas
Begby wrote:You might want to try maybe commenting your function to start with, I don't think anybody here is going to try and figure out what its supposed to be doing.
You're right
Here it is:

Code: Select all

<?

ponuda("03/01/2007", "03/12/2007", "2");

function ponuda($start_date, $end_date, $objekt)
{

     $broj_dana_u_periodu = array();

     $start_date_reserv = strtotime($start_date);//start date of reservation period
     $end_date_reserv = strtotime($end_date);//end date of reservation period
     $sezone = array('03/21/2007-03/31/2007', '03/11/2007-03/20/2007',
          '03/01/2007-03/10/2007', '02/01/2007-02/09/2007', '02/10/2007-02/28/2007');//array of seasons
     $cijene = array(57, 20, 15, 35, 48);//prices of each season

     //convering from date format mm/dd/yyyy to unix timestamp
     for ($i = 0; $i < count($sezone); $i++)
     {
          $temp = explode("-", $sezone[$i]);
          $temp[0] = round(strtotime($temp[0]) / 86400);
          $temp[1] = round(strtotime($temp[1]) / 86400);
          $broj_dana_u_periodu[] = $temp[0] . "-" . $temp[1];
     }

     // diference in days (unix timestamp format) between start && end date
     $dif_days_reserv = round(($end_date_reserv - $start_date_reserv) / 86400);

     if ($dif_days_reserv > 0)
     {
          // converting start date of reservation period into unix timestamp
          $poc_datum = round(strtotime($start_date) / 86400);
          // converting end date of reservation period into unix timestamp
          $kraj_datum = round(strtotime($end_date) / 86400) - 1;

          //COUNTING DAYS IN SEASON COMPARING WITH END DATE OF RESERVATION PERIOD

          for ($i = $poc_datum; $i <= $kraj_datum; $i++)
          {
               for ($j = 0; $j < count($broj_dana_u_periodu); $j++)
               {
                    $sez = explode("-", $broj_dana_u_periodu[$j]);
                    if (($i >= $sez[0]) && ($i <= $sez[1]))
                    {
                         $brojanje_cijena[$j]++;
                    }
               }

          }
     }
     //****************************************************************
     //***************************************************************
     //if uncomment this line result is Array ( [2] => 10 [1] => 1 )
     //WHY IS KEY 2 FIRST ONE AND NOT 0 ?????????
     //****************************************************************
     //****************************************************************

     //print_r ($brojanje_cijena);

     // put out some result
     for ($i = 0; $i < count($brojanje_cijena); $i++)
     {

          echo $brojanje_cijena[$i] . "<br>";
          if (($brojanje_cijena[$i] != 0) && ($cijene[$i] != 0))
          {
               $ukupno += $brojanje_cijena[$i] * $cijene[$i];
               $rezultat .= $brojanje_cijena[$i] . " days at " . $cijene[$i] . " = " . $brojanje_cijena
                    [$i] * $cijene[$i] . " €<br>";
          }
     }

     echo $rezultat;
     echo $ukupno;

}

?>

Posted: Wed Feb 28, 2007 8:46 am
by Begby
Unfortunately, looking at your code, I cannot determine why your function is not working correctly. It does appear overly complex though given the problem, perhaps you were thinking through the logic as you coded it.

I suggest you work out the logic first with a pencil and paper, then code it breaking it into sub functions if necessary.

One way you can do it is to take your seasons array and create an array of days/prices like this....

Code: Select all

// Do some magic with your seasons and get this (you might want to us unix time stamps instead of text dates)
// also I made up all these dates and numbers

$dates[1/10/07] = 20 ;
$dates[1/11/07] = 20 ;
$dates[1/12/07] = 20 ;
$dates[1/13/07] = 25 ;
$dates[1/14/07] = 25 ;
$dates[1/15/07] = 25 ;
$dates[1/16/07] = 30 ;
Then your input will be a range of dates, split that into an array of timestamps, then match them to your dates array

Code: Select all

// given a range of 1/11-14 we will end up with an array that looks like

$range[0] = 1/11/07 ;
$range[1] = 1/12/07;
$range[2] = 1/13/07 ;
$range[3] = 1/14/07 ;

// Loop through the days and a get a price for each one and store it into a new array
foreach($range as $date)
{
  $prices[$date] = $dates[$date] ;
}

// Now prices will look like this
$prices[1/11/07] = 20 ;
$prices[1/12/07] = 20 ;
$prices[1/13/07] = 25 ;
$prices[1/14/07] = 25 ;
No that you have your prices for each day of the stay you can loop throgh them and display them, and get a total, or whatever.