<?php
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();
echo "<br>".date("d-m-Y");
die();
?>


<?php
/*******EDIT LINES 3-8*******/
$DB_Server = "localhost"; //MySQL Server    
$DB_Username = "root"; //MySQL Username     
$DB_Password = "uSS10nl1n3";             //MySQL Password     
$DB_DBName = "bprsap";         //MySQL Database Name  
$DB_TBLName = "kredit"; //MySQL Table Name   
$filename = "excelfilename";         //File Name
/*******YOU DO NOT NEED TO EDIT ANYTHING BELOW THIS LINE*******/    
//create MySQL connection   
$sql = "Select * from $DB_TBLName";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database   
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());   
//execute query 
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());    
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");    
header("Content-Disposition: attachment; filename=$filename.xls");  
header("Pragma: no-cache"); 
header("Expires: 0");
/*******Start of Formatting for Excel*******/   
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");    
//end of printing column names  
//start while loop to get data
    while($row = mysql_fetch_row($result))
    {
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
            if(!isset($row[$j]))
                $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]".$sep;
            else
                $schema_insert .= "".$sep;
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        print "\n";
    }   
die();
?>


<?php
	//aktifkan session
	if(!isset($_SESSION)){session_start();}
	
	$filterRulesx=(isset($_SESSION['filterRules']) ? $_SESSION['filterRules']:'');
	$ss=json_decode($filterRulesx);		
	foreach($ss as $value ){
		if (trim($value->field)=='tgl'){
			$tgl12 = explode(":", $value->value);
		}
	}
	echo $tgl12[0].' '.$tgl12[1];
	die();
?>


<?php
echo date('Y');
die();
?>

<?php
//UPLOAD data absensi ke database - hensap
//Source => http://www.infragistics.com/community/blogs/taz_abdeali/archive/2012/02/08/netadvantage-for-jquery-with-php-101.aspx
//aktifkan session
if(!isset($_SESSION)){session_start();}

//open connection
include ('connect-DB.php');


$line = file("temp/1_attlog.txt");//file in to an array mulai dari 0
//echo $lines[70955]; //line 2
$jmlrow = count( $line );

//cek ada kah data???
if ($jmlrow>0){

	$thnx='2018';
	$blnx='03'; //kosongkan maka all bln
	$tglx=''  ; //kosongkan maka all bln
	$searchword = $thnx.(($blnx!='')?'-'.$blnx:'').(($tglx!='')?'-'.$tglx:''); //format '2018-03';
	echo 'data yg dicari='.$searchword.'<br>';
	$matches = array_filter($line, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });

	if (!empty($matches)){
		$query="delete from absen_mesin where year(tgl_jam)=$thnx and month(tgl_jam)=$blnx; \n";
		//$rs = mysqli_multi_query($conn_login, $query);

		//cek isi $matches;
		//print_r ($matches);

		//untuk ambil nilai key saat di temukan
		$urutanke = array_keys($matches);

		//baca isi key array $matches
		echo 'urutan no mulai dari 0, ini array ke:'.$urutanke[0].'<br>';
		
		//baca isi $line sesuai dengan no record yg di temukan pertama kali
		echo 'baca isi array tadi:'.$line[$urutanke[0]].'<br>';

		//total jml row yg ketemu sesuai $searchword
		echo 'jml record:'.count($matches).'<br>';
		//die();

		$totalrow=$urutanke[0]+count($matches); //no urut +total baris=jumlah data text, tapi secara array tetap di mulai dari 0, maka hasil $jmlrow musti di kurang 1
		for ($jmlrow=$urutanke[0]; $jmlrow <= $totalrow-1; $jmlrow++){
			$data			=preg_split('/\s+/', $line[$jmlrow]);	
			$idx      =substr(1000000000000+$data[1],-12);
			$tglx    = "STR_TO_DATE('".$data[2]."','%Y-%m-%d')";
			$jamx     = "STR_TO_DATE('".$data[3]."','%H:%i:%s')";
			$tgl_jamx= "STR_TO_DATE('".$data[2].' '.$data[3]."','%Y-%m-%d %H:%i:%s')";

      //membuat dalam array supaya upload datanya cepat, sumber=>https://www.phpflow.com/php/insert-php-array-into-mysql-table/
 			$id_valx[]= $idx;
			$hr_valx[]= $tgl_jamx;
			$gabungan[]="('$idx',$tgl_jamx,$tglx,$jamx)";
		}
		//echo $query."<br>";

		//$query="INSERT IGNORE INTO absen_mesin(`id`  ,`tgl_jam`) VALUES	('$idx',STR_TO_DATE('$tgl_jamx','%Y-%m-%d %H:%i:%s')); \n";
		$query.="INSERT IGNORE INTO absen_mesin(`id`  ,`tgl_jam`, `tgl`, `jam`) VALUES	".implode(',',$gabungan)."; \n";
		//echo $query;die();

		$rs = mysqli_multi_query($conn_login, $query);	
	}else{
		echo 'error...,'.$searchword.' tidak di temukan<br>';
	}
}

/*

//jam masuk dan kluar

SELECT 
DATE_FORMAT(a.tgl,'%d-%m-%Y') AS tgl,
a.id,
a.nama,
IF(ISNULL(b.masuk),'-',IF(b.masuk=b.masuk,IF(b.masuk<TIME('09:00'),b.masuk,'-') ,b.masuk)) AS masuk,
IF(ISNULL(b.pulang),'-',IF(b.masuk=b.pulang,IF(b.pulang>TIME('09:00'),b.pulang,'-') ,b.pulang)) AS pulang FROM
(
  -- buat gabungan user per tgl 
  SELECT a.tgl,b.id,b.nama FROM 
    (SELECT tgl FROM absen_mesin GROUP BY tgl) a, (SELECT * FROM absen_user WHERE STATUS=1) b
  ORDER BY a.tgl,b.nama
)a
LEFT JOIN (SELECT id,tgl,MIN(jam) AS masuk, MAX(jam) AS pulang FROM absen_mesin GROUP BY id,tgl) b ON a.tgl=b.`tgl` AND a.id=b.`id`

*/


die();
?>

<?php
echo "test";
?>
