Created
May 18, 2017 21:09
-
-
Save mustafat0k/049c9a5caf498e69363b05c4f0b006cf to your computer and use it in GitHub Desktop.
LaravelCarbon Taksit günleri hesaplaması
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
.. | |
*/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Taksit Günlerini Bul | |
|-------------------------------------------------------------------------- | |
| | |
| Algoritmayı sadeleştirirsek. | |
| 1) Başlangıç ayını bul, bu ay dahil taksit sayısı kadar sonraki her ay ödeme olacak. | |
| 2) Sonraki aya geç, tatil günleri ve haftasonları hariç günleri bul, o ayki güne en yakın günü bul. | |
| | |
*/ | |
$baslangic_tarih_str = "01.03.2017"; | |
$taksit_adet = 20; | |
$baslangic_tarihi = \Carbon\Carbon::createFromFormat('d.m.Y', $baslangic_tarih_str); | |
$tatil_gunleri = [ | |
'2017-02-10', '2017-03-04','2017-03-01','2017-04-27','2017-06-27', | |
'2017-02-11', '2017-03-05','2017-03-02','2017-04-28','2017-06-28', | |
'2017-02-12', '2017-03-12','2017-03-30','2017-05-29','2017-06-29', | |
'2017-02-13', '2017-02-27','2017-03-31','2017-05-30','2017-06-30', | |
'2017-02-14', '2017-02-28','2017-04-01','2017-06-26', | |
]; | |
$taksit_gunleri = []; | |
$bt = $baslangic_tarihi->copy(); | |
$bt_gun = substr('0'.$baslangic_tarihi->day, -2); | |
for ($taksit_adet; $taksit_adet >= 0; $taksit_adet--) { | |
$aybasi = $bt->startOfMonth()->copy(); | |
$aysonu = $bt->endOfMonth()->copy(); | |
$bu_gun = substr('0'.$bt->day, -2); | |
$bu_ay = substr('0'.$aybasi->month, -2); | |
$bu_yil = $aybasi->year; | |
$daterange = new DatePeriod($aybasi, \Carbon\CarbonInterval::day(), $aysonu); | |
$months = []; | |
foreach ($daterange as $day) { | |
if(!in_array($day->format('Y-m-d'), $tatil_gunleri) && !in_array($day->format('D'), ['Sat', 'Sun']) ) { | |
$months[] = $day->format('Y-m-d'); | |
} | |
} | |
if(in_array("$bu_yil-$bu_ay-$bt_gun", $months)){ | |
$taksit_gunleri[] = "$bu_yil-$bu_ay-$bt_gun"; | |
} else { | |
for ($i=0; $i <= 31 ; $i++) { | |
$gun = $bt_gun; | |
$arti_gun = substr('0'.($gun + $i), -2); | |
$eksi_gun = substr('0'.($gun - $i), -2); | |
if (in_array("$bu_yil-$bu_ay-$arti_gun", $months)){ | |
$taksit_gunleri[] = "$bu_yil-$bu_ay-$arti_gun"; | |
break; | |
} else if (in_array("$bu_yil-$bu_ay-$eksi_gun", $months)){ | |
$taksit_gunleri[] = "$bu_yil-$bu_ay-$eksi_gun"; | |
break; | |
} | |
} | |
} | |
$bt = $bt->endOfMonth()->addDay()->endOfMonth()->copy(); | |
} | |
var_dump($taksit_gunleri); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment