wah mantap tuch om,bisa saja dengan 4 field. selebihnya diolah oleh query database + PHP.
database (SQL) juga punya bahasa (syntax) sendiri, lho.
CREATE TABLE IF NOT EXISTS `data` (
`id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`awal` datetime DEFAULT NULL,
`akhir` datetime DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
<?php
// file : config.php
//
// menggunakan PDO (atau MySQLi) adalah "best practice"
//
$db_host = 'localhost';
$db_name = 'jadwal'; // sesuaikan
$db_user = 'test'; // sesuaikan
$db_pass = 'test'; // sesuaikan
$dns = "mysql:host=$db_host;dbname=$db_name";
mb_internal_encoding("UTF-8");
if(!extension_loaded('pdo')) die ('Oops!');
try {
$dbh = new PDO($dns,$db_user,$db_pass,array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_EMULATE_PREPARES, false)
);
} catch(PDOException $e) {
die($e->getMessage());
}
<?php
// file : dummy.php
require("config.php");
function random_menit(){
return $rand = (rand(20,80) * 60);
}
$start = strtotime("2014-09-01 08:00:00");
$user = 1;
$total = 100;
$sql = "INSERT INTO `data`(`user_id`, `awal`, `akhir`) VALUES (:user, :awal, :akhir);";
$sth = $dbh->prepare($sql);
for($i = 0; $i < $total; $i++) {
$durasi = random_menit();
$end = $start + $durasi;
$awal = date("Y-m-d G:i:s", $start);
$akhir = date("Y-m-d G:i:s", $end);
$sth->bindParam(':user', $user);
$sth->bindParam(':awal', $awal);
$sth->bindParam(':akhir', $akhir);
$sth->execute();
$start = $start + 86400;
}
echo "Populating dummy data... Done.";
<?php
// file : index.php
require("config.php");
// hitung durasi
function durasi_login($awal, $akhir){
$d1 = new DateTime($awal);
$d2 = new DateTime($akhir);
$interval = $d2->diff($d1);
return $interval->format('%d hari, %H jam, %I menit, %S detik');
}
// cari data user pada bulan ini
$query = "SELECT * FROM `data` WHERE `user_id` = 1 and date_format(`awal`,'%Y-%m')=date_format(now(), '%Y-%m')";
$sql = $dbh->prepare($query);
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);
// jumlah hari pada bulan ini
$days = date('t');
// ekstrak data (presentation)
for($i=0; $i < $days; $i++){
$num = $i+1;
$awal = $data[$i]["awal"];
$akhir = $data[$i]["akhir"];
$durasi = durasi_login($awal,$akhir);
echo "Tanggal $num : $awal - $akhir - $durasi<br />";
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>::Absen::</title>
</head>
<body background="background4.png">
<p></p>
<form action="simpanabsen.php" method="post" enctype="multipart/form-data" name="FMHS">
<table width="452" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#1605c0">
<tr>
<table width="50%" align="center" valign="top" border="0">
</th>
<tr>
<td ><table width="" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<tr><td width=""><b>ID </b></td><td width="11">:</td>
<td width="237"><input required="required" name="id" type="text" id="id" size="30" maxlength="30"></td>
</tr>
<tr>
<tr><td width=""><b>Nama </b></td><td width="11">:</td>
<td width="237"><input required="required" name="nama" type="text" id="nama" size="30" maxlength="30"></td>
</tr>
<tr>
<td><b> Status </b></td>
<td> : </td>
<td colspan="7"> <select name="status">
<option value="Jam Masuk"> Jam Masuk </option>
<option value="Jam Pulang"> Jam Pulang </option>
<option value="Izin"> Izin </option>
<option value="Sakit"> Sakit </option>
</select></td>
<tr>
<tr><td width=""><b>Keterangan </b></td><td width="11">:</td>
<td width="237"><input required="required" name="keterangan" type="text" id="keterangan" size="30" maxlength="30"></td>
</tr>
<tr><td colspan="3" align="center"><input name="input" type="submit" id="input" value="Input Absen">
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include "conn.php";
include "tanggal.php";
include "jam.php";
$id=$_POST['id'];
$nama=$_POST['nama'];
$status=$_POST['status'];
$jammasuk=$_POST['jammasuk'];
$jampulang = $_POST['jampulang'];
$tanggal=$_POST['tanggal'];
$keterangan=$_POST['keterangan'];
mysql_query("insert into absen(id,nama,status,jammasuk,jampulang,tanggal,keterangan) " .
"values('$id','$nama','$status','$jammasuk','$jampulang','$tanggal','$keterangan')") or die(mysql_error());
header("location:index.php");
?>
function input_absen($user_id){
global $dbh;
$now = date("Y-m-d G:i:s", time());
// cari data user tertanggal "hari ini"
$query = "SELECT * FROM `data` WHERE `user_id` = :user_id AND DATE_FORMAT(`awal`, '%Y-%m-%d') = CURDATE();";
$sql = $dbh->prepare($query);
$sql->bindParam(':user_id', $user_id);
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);
if(!empty($data[0]["awal"]) && !empty($data[0]["akhir"])){
// record user untuk hari ini sudah terisi. skip...
return;
}
if(empty($data[0]["awal"])){
// record user untuk hari ini belum ada. catat jam masuk.
$awal = $now;
$akhir = NULL;
}else{
// record user untuk hari ini sudah ada. catat jam keluar.
$awal = $data[0]["awal"];
$akhir = $now;
}
// rutin berikutnya. masukkan data.
// ...
// ...
// dst
}
<?php
$method = $_SERVER["REQUEST_METHOD"];
if($method == "POST"){
if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
// panggil fungsi pengecekan data & simpan
input_absen($user_id);
}else{
die('User ID kosong.');
}
}
// cari data user tertanggal "hari ini"
$query = "SELECT * FROM `data` WHERE `user_id` = :user_id AND DATE_FORMAT(`awal`, '%Y-%m-%d') = CURDATE();";
$sql = $dbh->prepare($query);
// cari data user tertanggal "hari ini"
$query = "SELECT * FROM `absen` WHERE `id` = :id AND DATE_FORMAT('jammasuk`, '%Y-%m-%d') = CURDATE();";
$sql = $dbh->prepare($query); --------->>Baris 10
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>::Absen::</title>
</head>
<body background="background4.png">
<p></p>
<form name='absen' action="simpanabsen.php" method="post">
<table width="452" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#1605c0">
<tr>
<table width="50%" align="center" valign="top" border="0">
</th>
<tr>
<td ><table width="" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<tr><td width=""><b>User ID </b></td><td width="11">:</td>
<td width="237"><input required="required" name="userid" type="text" id="userid" size="30" maxlength="30"></td>
</tr>
<tr>
<tr><td width=""><b>Nama </b></td><td width="11">:</td>
<td width="237"><input required="required" name="nama" type="text" id="nama" size="30" maxlength="30"></td>
</tr>
<tr>
<td><b> Status </b></td>
<td> : </td>
<td colspan="7"> <select name="status">
<option value="Jam Masuk"> Jam Masuk </option>
<option value="Jam Pulang"> Jam Pulang </option>
<option value="Izin"> Izin </option>
<option value="Sakit"> Sakit </option>
</select></td>
<tr>
<tr><td width=""><b>Keterangan </b></td><td width="11">:</td>
<td width="237"><input required="required" name="keterangan" type="text" id="keterangan" size="30" maxlength="30"></td>
</tr>
<tr><td colspan="3" align="center"><input name="submit" type="submit" id="submit" value="Input Absen">
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
,CREATE TABLE IF NOT EXISTS `absen` (
`id` int(10) unsigned NOT NULL
`userid` int(10) unsigned NOT NULL,
`nama` varchar(25) NOT NULL,
`status` varchar(25) NOT NULL,
`jammasuk` datetime DEFAULT NULL,
`jampulang` datetime DEFAULT NULL,
`tanggal` date NOT NULL,
`keterangan` varchar(55) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
<?php
$db_host = 'localhost';
$db_name = 'dbabsen';
$db_user = 'root';
$db_pass = 'pelajaran';
$dns = "mysql:host=$db_host;dbname=$db_name";
mb_internal_encoding("UTF-8");
if(!extension_loaded('pdo')) die ('Oops!');
try {
$dbh = new PDO($dns,$db_user,$db_pass,array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_EMULATE_PREPARES, false)
);
} catch(PDOException $e) {
die($e->getMessage());
}