Skip to content

Instantly share code, notes, and snippets.

@ahmetgungor
Last active February 20, 2018 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmetgungor/56c9c7fa658660135b23eebff4babec1 to your computer and use it in GitHub Desktop.
Save ahmetgungor/56c9c7fa658660135b23eebff4babec1 to your computer and use it in GitHub Desktop.
<?php
/**
* adasusbilisim[.]gmail.com
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Tarihhesapla {
private $msj = array();
/**
* eğer iki tarih arasındaki saat opsiyon_saat
* değerinden büyükse +1 Gün Eklenecek
*/
public $opsiyon_saat = 4;
public $holiday_day_percent =20;
public $holiday_day_json;
// ek opsiyonlar
public $additional_option; /*= '[[{"name":"bebekkoltugu","value":"5"}],[{"name":"sofor","value":"75"}],[{"name":"navigasyon","value":"5"}],[{"name":"tamkazasigortasi","value":"20"}]]';*/
public $fiyat = array('1-3' =>100,
'4-7' => 90,
'8-14'=> 75,
'15-25'=>65,
'26-'=>50,
);
function __construct()
{
}
function holiday_day_calculate($tarih1= null ,$tarih2= null,$fiyat=null,$durum='%')
{
if ($durum == '%')
{
$s = ($fiyat * $this->holiday_day_percent) / 100;
}
$json = json_decode($this->holiday_day_json,true);
$holiday = array();
foreach ($json as $key => $value) {
$holiday[] =str_replace('/','-',$key);
}
if (!empty($tarih2) and !empty($tarih1)) {
$list = $this->twodaysBetween($tarih1,$tarih2);
$i=0;
foreach ($list as $key => $value)
{
if (in_array($value,$holiday)) {
$key = array_search($value,$holiday);
$holiday[$key]."\n";
$i++;
}
}
return $s * $i;
}
}
// fiyat hesaplama
function price_calculation($tarih1=null,$tarih2=null)
{
$gun = $this->how_mony_days($tarih1,$tarih2);
$b['gun'] = $gun;
$ekler = $this->option_added();
print_r($this->fiyat);
foreach ($this->fiyat as $key => $value)
{
list($ba,$so) = @explode('-',$key);
echo $so;
if ($gun >=$ba && $gun <=$so) {
$b['toplam'] = (($value + $ekler['toplam']) * $gun);
if (!empty($this->holiday_day_json)) {
$b['bayram_tatil'] = $this->holiday_day_calculate($tarih1,$tarih2,$value);
}else{
$b['bayram_tatil'] ='';
}
$b['fiyat'] = $value;
}
if ($gun>= $ba && empty($so))
{
$b['toplam'] = (($value + $ekler['toplam']) * $gun);
if (!empty($this->holiday_day_json)) {
$b['bayram_tatil'] = $this->holiday_day_calculate($tarih1,$tarih2,$value);
}else{
$b['bayram_tatil'] ='';
}
$b['fiyat'] = $value;
}
}
$b['ekler'] = $ekler;
return $b;
}
//eklenen opsiyonlar
function option_added()
{
$opt = array();
if (!empty($this->additional_option)) {
$option = json_decode($this->additional_option,true);
$opt = array();
$toplam = 0;
foreach ($option as $key => $value)
{
$toplam =$toplam + $value[0]['value'];
$opt['list'][]= $value[0];
}
$opt['toplam'] = $toplam;
return $opt;
}else{
return $opt['toplam'] = 0;
}
}
// iki tarih arasında kaç gün
function how_mony_days($tarih1=null, $tarih2=null)
{
$tarih1_u = $this->datetounix($tarih1);
$tarih2_u = $this->datetounix($tarih2);
if ($tarih2_u > $tarih1_u)
{
$tarih2 = date_create(str_replace('/','-',$tarih2));
$tarih1 = date_create(str_replace('/','-',$tarih1));
$diff =(array) date_diff($tarih1,$tarih2);
print_r($diff);
$gun = $diff['days'];
$saat = $diff['h'];
var_dump($this->opsiyon_saat > 0);
if ($this->opsiyon_saat > 0)
{
if ($this->opsiyon_saat <= $saat) {
return $gun + 1;
}else{
return $gun;
}
}else{
return $gun;
}
}else{
$msj[] = 'Bitiş Tarihi Başlangıç tarihinden Önce Olamaz';
}
}
// eğer alış saati teslim tarihini geçerse bu fonksiyona 1 gün daha eklenecek;
function twodaysBetween($tarih1,$tarih2)
{
$tarih1 = $this->datetounix($tarih1);
$tarih2 = $this->datetounix($tarih2);
$j = 0;
$liste = array();
for ($i=$tarih1; $i < $tarih2 ; $i = $i +86400)
{
$j++;
$liste[] = date('d-m-Y',$i);
}
return $liste;
}
function sezon_ktn($tarih1=null,$tarih2=null)
{
$list = $this->twodaysBetween(date('15/05/Y h:i'),date('16/09/Y h:i'));
$sezon = $this->twodaysBetween($tarih1,$tarih2);
$i=0;
foreach ($list as $key => $value)
{
if (in_array($value,$sezon)) {
$key = array_search($value,$sezon);
$sezon[$key]."\n";
$i++;
}
}
return $i;
}
// dd-mm-yyyy hh:ii
//tarihi ve saati unix time dönüştürür.
function datetounix($date_time=null,$seperator='/',$timesep=':')
{
$rex = '@([0-9]{2})'.$seperator.'([0-9]{2})'.$seperator.'([0-9]{4}) ([0-9]{2})'.$timesep.'([0-9]{2})@si';
preg_match($rex,$date_time,$o);
return mktime($o[4],$o[5],'00',$o[2],$o[1],$o[3]);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment