Skip to content

Instantly share code, notes, and snippets.

@sarpdorukaslan
Last active August 29, 2015 14:19
Show Gist options
  • Save sarpdorukaslan/33cf56b2017f7807d48d to your computer and use it in GitHub Desktop.
Save sarpdorukaslan/33cf56b2017f7807d48d to your computer and use it in GitHub Desktop.
Araç Kiralama İçin Fiyat Hesaplama
<?php
Class PriceCalculate {
public $defaultPrice;
public $startDate;
public $endDate;
public $maxToleranceHour = 3;
public $startTime;
public $endTime;
public $priceList;
public $rentDay;
public $rentHour;
public $calculatedRentDay = 0;
public $calculatedPrice = 0;
function calculatePrice()
{
$this->calculateRentDay();
$this->calculatePriceFromPriceList();
return $this;
}
function calculatePriceFromPriceList()
{
$tempBookStart = new DateTime($this->startDate);
$tempBookEnd = new DateTime($this->startEnd);
foreach($this->priceList as $price)
{
$tempStart = new DateTime($price['startDate']);
if($tempBookStart->getTimestamp()<$tempStart->getTimestamp())
{
$calculateStart = $tempStart;
}
else
{
$calculateStart = $tempBookStart;
}
$tempEnd = new DateTime($price['endDate']);
if($tempBookEnd->getTimestamp()<$tempEnd->getTimestamp())
{
$calculateEnd = $tempEnd;
}
else
{
$calculateEnd = $tempBookEnd;
}
$tempDiffDay = $calculateEnd->diff($calculateStart)->format('%a');
$this->calculatedPrice += $tempDiffDay * $price['price'];
$this->calculatedRentDay += $tempDiffDay;
}
$restDay = $this->rentDay - $this->calculatedRentDay;
$this->calculatedPrice += $restDay * $this->defaultPrice;
}
function calculateRentDay()
{
$date1 = new DateTime($this->startDate." ".$this->startTime);
$date2 = new DateTime($this->endDate." ".$this->endTime);
$diff = $date2->diff($date1);
$this->rentDay = $diff->format('%a');
$this->rentHour = $diff->h;
if($this->rentHour>=$this->maxToleranceHour)
{
$this->rentDay += 1;
$this->endTime = $this->startTime;
$this->endDate = $date2->add(new DateInterval('P1D'))->format('Y-m-d');
}
}
}
$priceList[] = array('startDate'=>'2015-04-03','endDate'=>'2015-06-02',,'minDay'=>30,'maxDay'=>999999,'price'=>110);
$priceList[] = array('startDate'=>'2015-05-03','endDate'=>'2015-09-03',,'minDay'=>30,'maxDay'=>999999,'price'=>120);
$priceList[] = array('startDate'=>'2015-09-04','endDate'=>'2016-01-02',,'minDay'=>30,'maxDay'=>999999,'price'=>90);
$price = new PriceCalculate();
$price->maxToleranceHour = 3;
$price->priceList = $priceList;
$price->startDate = '2015-05-01';
$price->startTime = '09:00';
$price->endDate = '2015-10-22';
$price->endTime = '19:00';
$price->defaultPrice = 130;
$price->calculatePrice();
echo "<pre>";
print_r($price);
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment