<?php

class Date
{
	public function selisihTanggal($tgl1, $tgl2)
	{
		$pecah1 = explode("-", $tgl1);
		$date1 = $pecah1[2];
		$month1 = $pecah1[1];
		$year1 = $pecah1[0];

		$pecah2 = explode("-", $tgl2);
		$date2 = $pecah2[2];
		$month2 = $pecah2[1];
		$year2 =  $pecah2[0];

		$jd1 = GregorianToJD($month1, $date1, $year1);
		$jd2 = GregorianToJD($month2, $date2, $year2);
		
		return $jd2 - $jd1;
	}
	
	public function nextMonth($i, $tgl)
	{
		$thn = date('Y', strtotime($tgl));
		$bln = date('m', strtotime($tgl));
		$tgl = date('d', strtotime($tgl));

		$i = $bln + $i;

		if($i <= 12){
			$next = $thn.'-'.$i.'-'.$tgl;
		}else{
			$next = ($thn + 1).'-'.($i - 12).'-'.$tgl;
		}
		
		return $this->setTgl($next);
	}
	
	protected function setTgl($tgl)
	{
		$thn = date('Y', strtotime($tgl));
		$bln = date('m', strtotime($tgl));
		$tgl = date('d', strtotime($tgl));
		
        $i = cal_days_in_month(CAL_GREGORIAN, $bln, $thn);
		
		if($tgl > $i){
			$tgl = $i;
		}
		
		return date('Y-m-d', strtotime($thn.'-'.$bln.'-'.$tgl));
	}
	
	public function nextDayDate($date, $n)
	{
		$exp = mktime(0, 0, 0, date("m", strtotime($date)), date("d", strtotime($date)) + $n, date("Y", strtotime($date)));
		return date("Y-m-d", $exp);
	}
	
	public function getPenjumlahanTanggal($date, $bln, $tgl, $thn)
	{
		$exp = mktime(0, 0, 0, date("m", strtotime($date)) + $bln, date("d", strtotime($date)) + $tgl, date("Y", strtotime($date)) + $thn);
		return date("Y-m-d", $exp);
	}
	
	public function getPenguranganTanggal($date, $bln, $tgl, $thn)
	{
		$exp = mktime(0, 0, 0, date("m", strtotime($date)) - $bln, date("d", strtotime($date)) - $tgl, date("Y", strtotime($date)) - $thn);
		return date("Y-m-d", $exp);
	}
	
	public function getConersiTgl($tgl)
	{
		$date = $this->getBulan();
		$ex  = explode('/', $tgl);
		$bln = (int)$ex[1];
		
		$tgl = $ex[0].'-'.$date[$bln].'-'.$ex[2];
		
		$tgl = date('Y-m-d', strtotime($tgl));
		return $tgl;
	}
	
	public function getBulan()
	{
		$data = array(
					'1' => 'Jan',
					'2' => 'Feb',
					'3' => 'Mar',
					'4' => 'Apr',
					'5' => 'May',
					'6' => 'Jun',
					'7' => 'Jul',
					'8' => 'Aug',
					'9' => 'Sep',
					'10' => 'Oct',
					'11' => 'Nov',
					'12' => 'Dec',
				);
		return $data;
	}
	
	public function getMonth()
	{
		$data = array(
					'1' => 'JAN',
					'2' => 'FEB',
					'3' => 'MRT',
					'4' => 'APR',
					'5' => 'MEI',
					'6' => 'JUN',
					'7' => 'JUL',
					'8' => 'AGT',
					'9' => 'SEP',
					'10' => 'OKT',
					'11' => 'NOV',
					'12' => 'DES',
				);
		return $data;
	}
	
	public function getDay()
	{
		$data = array(
					'SUN' => 'MINGGU',
					'Mon' => 'SENIN',
					'Tue' => 'SELASA',
					'Wed' => 'RABU',
					'Thu' => 'KAMIS',
					'Fri' => 'JUMAT',
					'Sat' => 'SABTU',
				);
		return $data;
	}
	
	public function setTglNull($tgl)
	{
		if(!empty($tgl)){
			return date('Y-m-d H:i:s', strtotime($tgl));
		}
		return 'null';
	}
	
	public function getTglNull($tgl)
	{
		if(!empty($tgl)){
			return date('d-M-Y', strtotime($tgl));
		}
		return;
	}
	
	public function getSerializeDate($tgl)
	{	
		if(!empty($tgl) and $tgl != '1970-01-01 00:00:00'){
			$tgl = date('d-M-Y', strtotime($tgl));
		}else{
			$tgl = '';
		}
		return $tgl;
	}
	
	public function serialDate($data)
	{
		$data = explode("/", $data);
		$date = $data[2].'-'.$data[1].'-'.$data[0];
		$date = date("Y-m-d", strtotime($date));

		if(!empty($date) and $date != '1970-01-01'){
			$tgl = date('Y-m-d', strtotime($date));
		}else{
			$tgl = '';
		}
		return $tgl;
	}
	
	public function blnIndo()
	{
		$data = array(
					'1' => 'Januari',
					'2' => 'Februari',
					'3' => 'Maret',
					'4' => 'April',
					'5' => 'Mei',
					'6' => 'Juni',
					'7' => 'Juli',
					'8' => 'Agustus',
					'9' => 'September',
					'10' => 'Oktober',
					'11' => 'November',
					'12' => 'Desember',
				);
		return $data;
	}
	
	public function tglIndo($tgl)
	{
		$tgl = date("d/m/Y", strtotime($tgl));
		$exp = explode("/", $tgl);
		$idB = (int)$exp[1];
		$bln = $this->blnIndo();
		
		return $exp[0].' '.$bln[$idB].' '.$exp[2];
	}
}