<?php

require_once("dbconn.php");

function hitungumur($tgl_lahir, $tgl_skrg) {
    $time1 = strtotime($tgl_lahir);
    $time2 = strtotime($tgl_skrg);

    $timediff = $time2 - $time1;
    $totaldiff = $timediff / 3600 / 24;
    $daydiff = ceil($timediff / 3600 / 24);
    $sisa = $totaldiff - $daydiff;
    $oneday = 3600 * 24;
    $hasil = array();
    for ($i = 0; $i < $daydiff; $i++) {
        $hasil[] = strtotime(date('d-m-Y', $time1 + ($oneday * $i)));
        $tgl = $time1 + ($oneday * $i * 2);
    }
    $hasil[0] = $time1;
//$hasil[2]=$time2;
    $totalwaktu = array();
    foreach ($hasil as $value => $key) {

        if ($value > 0) {
            $totalwaktu[date('d-m-Y', $hasil[$value - 1])] = ($hasil[$value] - $hasil[$value - 1]) / 3600;
        }
    }
    $lastdiff = ($time2 - $hasil[count($hasil) - 1]) / 3600;
    $totalwaktu[date('d-m-Y', $hasil[count($hasil) - 1])] = $lastdiff;
    return $totalwaktu;
}

function pgsql2phpDate($mysqlStr) {
    $s = explode("-", $mysqlStr);
    return getdate(mktime(0, 0, 0, $s[1], $s[2], $s[0]));
}

function pgsql2mktime($mysqlStr) {
    $s = explode("-", $mysqlStr);
    return mktime(0, 0, 0, $s[1], $s[2], $s[0]);
}

function errmsg($title, $msg, $tipe) {
    echo "<DIV CLASS=$tipe>";
    echo "<B>$title</B><BR>$msg";
    echo "</DIV>";
}

function info($title, $msg) {
    echo "<DIV CLASS=INFO>";
    echo "<B>$title</B><BR>$msg";
    echo "</DIV>";
}

function icon($name, $alt = "") {
    return "<IMG ALT='$alt' BORDER=0 SRC='images/icon-{$name}.png' title='$alt' >";
}

function img($src, $onclick, $alt = "") {
    return "<IMG ALT='$alt' onclick=$onclick BORDER=0 valign='bottom' SRC='$src' title='$alt' >";
}

function title($str) {
    echo "<DIV ALIGN=LEFT CLASS=FORM_TITLE><B>&nbsp;&nbsp;&nbsp;$str</B></DIV>";
}

function title_print($title, $align = 'left') {
    if ($GLOBALS['print']) {
        echo "<table width='100%' cellspacing=0 cellpadding=2><tr><td CLASS='FORM_TITLE' align='$align' ><b>$title</b></td></tr></table>\n";
    } else {
        echo "<table width='100%' cellspacing=0 cellpadding=2><tr><td CLASS='FORM_TITLE'><img src='images/sales-report-icon.png' width='65' align='absmiddle' ><b>$title</b></td>\n";
        echo "<td width=1 align=right CLASS='FORM_TITLE'><a href=\"Excelprint.php?{$_SERVER['QUERY_STRING']}\"><img border=0 src=\"images/Microsoft-Excel-icon.png\" width='65'></a></td>\n";
        echo "<td width=1 align=right CLASS='FORM_TITLE'><a href=\"javascript:printPage()\"><img border=0 src=\"images/Microsoft-Print-icon.png\" width='65'></a></td>\n";
        echo "</tr></table><br/>\n";
        echo "<script language=\"JavaScript\">\n";
        echo "function printPage() {\n";
        echo "  oWin = window.open('print.php?{$_SERVER['QUERY_STRING']}', 'zWin', 'width=800,height=600,scrollbars=yes');\n";
        echo "  oWin.focus();\n";
        echo "}\n";
        echo "function printExcel() {\n";
        echo "  oWin = window.open('Excelprint.php?{$_SERVER['QUERY_STRING']}', 'zWin', 'width=800,height=600,scrollbars=yes');\n";
        echo "  oWin.focus();\n";
        echo "}\n";
        echo "</script>\n";
    }
}

function title_excel($link) {
    if ($GLOBALS['print']) {
        echo "<table width='100%' cellspacing=0 cellpadding=2><tr><td CLASS='FORM_TITLE' align='left' ></td></tr></table>\n";
    } else {
        echo "<table width='100%' cellspacing=0 cellpadding=2><tr><td CLASS='PAGE_TITLE'></td>\n";
        echo "<td width=1 align=right><a href=\"Excelprint.php?p=$link\"><img border=0 src=\"icon/Excel-22.gif\"></a></td>\n";
        echo "</tr></table>\n";
        echo "<script language=\"JavaScript\">\n";
        //   echo "function printPage() {\n";
        //   echo "  oWin = window.open('print.php?{$_SERVER['QUERY_STRING']}', 'zWin', 'width=800,height=600,scrollbars=yes');\n";
        //   echo "  oWin.focus();\n";
        //   echo "}\n";
        echo "</script>\n";
    }
}

function umur($usia) {
    $search = array("'years'", "'year'",
        "'mons'", "'mon'",
        "'Bulan'",
        "'days'", "'day'",
        "'hari'",
        "'Tahun'");
    $replace = array("Tahun", "Tahun",
        "Bulan", "Bulan",
        "Bulan",
        "Hari", "Hari",
        "Hari",
        "Tahun");
    $text = preg_replace($search, $replace, $usia);
    return ($text);
}

function nav_db2($start, $rec_no, $max_view, $go_to, $par)
/*
  Fungsi Navigasi database : start = Awal Record , Posisi record, jml data yang di tampilkan, next appl, parameter lain
 */ {
    global $first_nya, $prev_nya, $next_nya, $last_nya;
    if (($start - $max_view) >= 0) {
        $ke = $start - $max_view;
        $prev_nya = "<a href='" . $go_to . "&rec=" . $ke . $par . "'><img border=0 src='images/moveprev.png'></a>";
        $first_nya = "<a href='" . $go_to . "&rec=0" . $par . "'><img border=0 src='images/movefirst.png'></a>";
    } else {
        $prev_nya = "<img border=0 src='images/moveprev-d.png' >";
        $first_nya = "<img border=0 src='images/movefirst-d.png'>";
    }

    if (($start + $max_view) <= $rec_no) {
        $ke = $start + $max_view;
        $next_nya = "<a href='" . $go_to . "&rec=" . $ke . $par . "'><img border=0 src='images/movenext.png'></a>";
        $ke = ($rec_no - $max_view) + 1;
        $last_nya = "<a href='" . $go_to . "&rec=" . $ke . $par . "'><img border=0 src='images/movelast.png'></a>";
    } else {
        $next_nya = "<img border=0 src='images/movenext-d.png'>";
        $last_nya = "<img border=0 src='images/movelast-d.png'>";
    }
}

function tanggal($tgl){
    $tanggal = substr($tgl,8,2);
    $bulan = getBulan(substr($tgl,5,2));
    $tahun = substr($tgl,0,4);
    return $tanggal.' '.$bulan.' '.$tahun;
}
 
function getBulan($bln){ 
    switch ($bln){
        case 1: return "Januari"; break;
        case 2: return "Februari"; break;
        case 3: return "Maret"; break;
        case 4: return "April"; break;
        case 5: return "Mei"; break;
        case 6: return "Juni"; break;
        case 7: return "Juli"; break;
        case 8: return "Agustus"; break;
        case 9: return "September"; break;
        case 10: return "Oktober"; break;
        case 11: return "November"; break;
        case 12: return "Desember"; break;
    }
}
?>
