Minggu, 18 Agustus 2024

Selisih Waktu Berjalan

Berikut adalah fungsi PHP yang menghitung selisih hari antara tanggal saat ini dengan tanggal 2024-08-22. Jika tanggal saat ini kurang dari 2024-08-22, maka fungsi akan mengembalikan jumlah hari yang tersisa. Jika tanggal saat ini sudah melewati 2024-08-22, maka fungsi tidak akan menampilkan apa pun.


function hitungSelisihHari($targetDate = '2024-08-22') {
    $currentDate = date('Y-m-d');
    
    // Konversi tanggal menjadi objek DateTime
    $currentDateTime = new DateTime($currentDate);
    $targetDateTime = new DateTime($targetDate);
    
    // Hitung selisih hari
    if ($currentDateTime < $targetDateTime) {
        $interval = $currentDateTime->diff($targetDateTime);
        echo "Selisih hari: " . $interval->days . " hari";
    } else {
        // Jika tanggal sudah lewat, tidak tampilkan apapun
        echo "";
    }
}

// Contoh penggunaan
hitungSelisihHari(); 



function is24HoursPassed($bookingDate) {
    // Convert the booking date to a timestamp
    $bookingTimestamp = strtotime($bookingDate);

    // Get the current timestamp
    $currentTimestamp = time();

    // Calculate the difference in seconds
    $differenceInSeconds = $currentTimestamp - $bookingTimestamp;

    // Check if the difference is greater than or equal to 24 hours (86400 seconds)
    if ($differenceInSeconds >= 86400) {
        return 1;
    } else {
        return 0;
    }
}


function spot($str,$N){
return wordwrap($str,$N,"<br>\n");
}
function kirimWA($HP,$PESAN){
echo "$HP,$PESAN";
$curl = curl_init(); 
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.fonnte.com/send',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST', 
  CURLOPT_POSTFIELDS => array(
'target' => $HP,
'message' => $PESAN, 
'countryCode' => '62', //optional
),
  CURLOPT_HTTPHEADER => array(
'Authorization: NzTx3XXXXX'
  ),
)); 
$response = curl_exec($curl);
if (curl_errno($curl)) {
  $error_msg = curl_error($curl);
}
curl_close($curl);

if (isset($error_msg)) {
echo $error_msg;
}
echo $response;
}


function sentWA($conn, $id_pinjam, $id_anggota, $telepon, $pesan, $keterangan)
{
  date_default_timezone_set("Asia/Jakarta");
  $tanggal = date("Y-m-d");
  $jam = date("H:i:s");

  $sql = "INSERT INTO `tb_wa` (`idw`, `tanggal`, `jam`, `id_pinjam`,`id_anggota`, `telepon`, `pesan`, `keterangan`) VALUES ('', '$tanggal', '$jam', '$id_pinjam','$id_anggota', '$telepon', '$pesan', '$keterangan')";
  $s = false;
  $conn->autocommit(FALSE);
  try {
    $rs = $conn->query($sql);
    if ($rs) {
      $conn->commit();
      $last_inserted_id = $conn->insert_id;
      $affected_rows = $conn->affected_rows;
      $s = true;
    }
  } catch (Exception $e) {
    echo 'fail: ' . $e->getMessage();
    $conn->rollback();
  }
  $conn->autocommit(TRUE);
  return $s;
}
function RP($rupiah){
return number_format($rupiah, "2", ",", ".");
}  


function getR($digits) {
  $temp = "";
  for ($i = 0; $i < $digits; $i++) {
    $temp .= rand(0, 9);
  }
  return (int)$temp;
}
function saveWA2($conn,$id_pinjam,$id_anggota,$telepon,$pesan){
return 0;
}

function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$gab="";
    for ($i = 0; $i < 8; $i++) {//8 karakter
        $n = rand(0, count($alphabet)-1);
       // $pass[$i] = $alphabet[$n];
   $gab.= $alphabet[$n];;
    }
    return $gab;
}
function saveWA($conn,$id_pinjam,$id_anggota,$telepon,$pesan){
date_default_timezone_set("Asia/Jakarta");
$tanggal=date("Y-m-d");
$jam=date("H:i:s");
$sql="INSERT INTO `tb_wa` ( `tanggal`, `jam`, `id_pinjam`, `id_anggota`, `telepon`, `pesan`, `keterangan`) VALUES ('$tanggal', '$jam', '$id_pinjam', '$id_anggota', '$telepon', '$pesan', '')";
$pro=process($conn,$sql);
$reply = ["message" => "$pesan",];
kirimWA($telepon,$pesan);
return $pro;
}


function terbilang($i){
  $huruf = array("", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
 
  if ($i < 12) return " " . $huruf[$i];
  elseif ($i < 20) return terbilang($i - 10) . " Belas";
  elseif ($i < 100) return terbilang($i / 10) . " Puluh" . terbilang($i % 10);
  elseif ($i < 200) return " Seratus" . terbilang($i - 100);
  elseif ($i < 1000) return terbilang($i / 100) . " Ratus" . terbilang($i % 100);
  elseif ($i < 2000) return " Seribu" . terbilang($i - 1000);
  elseif ($i < 1000000) return terbilang($i / 1000) . " Ribu" . terbilang($i % 1000);
  elseif ($i < 1000000000) return terbilang($i / 1000000) . " Juta" . terbilang($i % 1000000);   


function BAL($tanggal){
$arr=explode(" ",$tanggal);
if($arr[1]=="Januari"||$arr[1]=="January"){$bul="01";}
else if($arr[1]=="Februari"||$arr[1]=="February"){$bul="02";}
else if($arr[1]=="Maret"||$arr[1]=="March"){$bul="03";}
else if($arr[1]=="April"){$bul="04";}
else if($arr[1]=="Mei"||$arr[1]=="May"){$bul="05";}
else if($arr[1]=="Juni"||$arr[1]=="June"){$bul="06";}
else if($arr[1]=="Juli"||$arr[1]=="July"){$bul="07";}
else if($arr[1]=="Agustus"||$arr[1]=="August"){$bul="08";}
else if($arr[1]=="September"){$bul="09";}
else if($arr[1]=="Oktober"||$arr[1]=="October"){$bul="10";}
else if($arr[1]=="November"){$bul="11";}
else if($arr[1]=="Nopember"){$bul="11";}
else if($arr[1]=="Desember"||$arr[1]=="December"){$bul="12";}
return "$arr[2]-$bul-$arr[0]";


function BALP($tanggal){
$arr=explode(" ",$tanggal);
if($arr[1]=="Jan"){$bul="01";}
else if($arr[1]=="Feb"){$bul="02";}
else if($arr[1]=="Mar"){$bul="03";}
else if($arr[1]=="Apr"){$bul="04";}
else if($arr[1]=="Mei"){$bul="05";}
else if($arr[1]=="Jun"){$bul="06";}
else if($arr[1]=="Jul"){$bul="07";}
else if($arr[1]=="Agu"){$bul="08";}
else if($arr[1]=="Sep"){$bul="09";}
else if($arr[1]=="Okt"){$bul="10";}
else if($arr[1]=="Nov"){$bul="11";}
else if($arr[1]=="Nop"){$bul="11";}
else if($arr[1]=="Des"){$bul="12";}
return "$arr[2]-$bul-$arr[0]";


function WKT($sekarang){
if ($sekarang == "0000-00-00") {
$sekarang = date("Y-m-d");
}

$tanggal = substr($sekarang, 8, 2) + 0;
$bulan = substr($sekarang, 5, 2);
$tahun = substr($sekarang, 0, 4);

$judul_bln = array(1 => "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
$wk = $tanggal . " " . $judul_bln[(int)$bulan] . " " . $tahun;
return $wk;


function WKTP($sekarang){
$tanggal = substr($sekarang, 8, 2) + 0;
$bulan = substr($sekarang, 5, 2);
$tahun = substr($sekarang, 2, 2);

$judul_bln = array(1 => "Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des");
$wk = $tanggal . " " . $judul_bln[(int)$bulan] . "'" . $tahun;
return $wk;


function saveLog($conn,$id_pengunjung,$nama_pengunjung,$level,$catatan){
date_default_timezone_set("Asia/Jakarta");
$jam=date("H:i:s");
$tanggal = date("Y-m-d");
$sql="INSERT INTO `tb_bukutamu` (`id_bukutamu`, `id_pengunjung`, `nama_pengunjung`, `level`, `catatan`, `tanggal`, `jam`) 
VALUES ('', '$id_pengunjung', '$nama_pengunjung', '$level', '$catatan', '$tanggal', '$jam')";
process($conn, $sql);
return 1;

}

function process($conn, $sql){
$s = false;
$conn->autocommit(FALSE);
try {
$rs = $conn->query($sql);
if ($rs) {
$conn->commit();
$last_inserted_id = $conn->insert_id;
$affected_rows = $conn->affected_rows;
$s = true;
}
} catch (Exception $e) {
echo 'fail: ' . $e->getMessage();
$conn->rollback();
}
$conn->autocommit(TRUE);
return $s;
}

function getJum($conn, $sql){
$rs = $conn->query($sql);
$jum = $rs->num_rows;
$rs->free();
return $jum;
}

function getField($conn, $sql){
$rs = $conn->query($sql);
$rs->data_seek(0);
$d = $rs->fetch_assoc();
$rs->free();
return $d;
}

function getData($conn, $sql){
$rs = $conn->query($sql);
$rs->data_seek(0);
$arr = $rs->fetch_all(MYSQLI_ASSOC);
//foreach($arr as $row) {
//  echo $row['nama_kelas'] . '*<br>';
//}

$rs->free();
return $arr;
}

function getBukuDetail($conn, $kode){
//error_reporting(0);
$field = "judul";
$sql = "SELECT `$field` FROM `tb_buku` where `id_buku`='$kode'";
$rs = $conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}

function getAnggotaDetail($conn, $kode){
//error_reporting(0);
$field = "nama_anggota";
$sql = "SELECT `$field` FROM `tb_anggota` where `id_anggota`='$kode'";
$rs = $conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}

function getAdmin($conn, $kode){
//error_reporting(0);
$field = "nama_admin";
$sql = "SELECT `$field` FROM `tb_admin` where `id_admin`='$kode'";
$rs = $conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}

function getKategori($conn, $kode){
//error_reporting(0);
$field = "kategori";
$sql = "SELECT `$field` FROM `tb_kategori` where `id_kategori`='$kode'";
$rs = $conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}

function getBuku($conn, $kode){
//error_reporting(0);
$field = "judul";
$sql = "SELECT `$field` FROM `tb_buku` where `id_buku`='$kode'";
$rs = $conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}
function getListB($conn, $kode){
//error_reporting(0);
    $sql="select `id_buku` from `tb_detail` where `id_pinjam`='$kode' order by `idd` asc";
    $jum=getJum($conn,$sql);
$gab="";
if($jum > 0){
$arr=getData($conn,$sql);
foreach($arr as $d) {
$id_buku=$d["id_buku"];
$buku=getBuku($conn,$id_buku);
$gab.="$buku ($id_buku), ";
}
$gab.=" $jum buku";
}
else{$gab="Belum melakukan peminjaman buku...";}
return $gab;
}

function getListB2($conn, $kode){
// error_reporting(0);
    $sql="select `id_buku` from `tb_detail` where `id_pinjam`='$kode' order by `idd` asc";
    $jum=getJum($conn,$sql);
$gab="<ol>";
if($jum > 0){
$arr=getData($conn,$sql);
foreach($arr as $d) {
$id_buku=$d["id_buku"];
$buku=getBuku($conn,$id_buku);
$gab.="<li>$buku ($id_buku)</li>";
}
$gab.="</ol>$jum buku";
}
else{$gab="Belum melakukan peminjaman buku...";}
return $gab;
}
function getCek($conn, $id_anggota){
$sql = "SELECT `tb_detail`.`idd` FROM `tb_pinjam`,`tb_detail` where `tb_pinjam`.id_pinjam=`tb_detail`.id_pinjam  and tb_pinjam.`id_anggota`='$id_anggota' and (`tb_detail`.status_detail='Pinjam' or `tb_detail`.status_detail='Pesan')";
$jum=getJum($conn,$sql)+0;
return $jum;
}
function upBuku($conn, $id_buku){
$sql="select `stok` from `tb_buku` where `id_buku`='$id_buku'";
    $jum=getJum($conn,$sql);
if($jum > 0){
$d=getField($conn,$sql);
$stok=$d["stok"]+0;
if($stok<1){
$sql = "Update `tb_buku` set status='Tidak Tersedia' where id_buku='$id_buku'";
$pro=process($conn,$sql);
}
else{
$sql = "Update `tb_buku` set status='Tersedia' where id_buku='$id_buku'";
$pro=process($conn,$sql);
}
}
return 1;
}

#########################
index.php (Cek timer)

<?php
if (version_compare(phpversion(), "5.3.0", ">=")  == 1)
  error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
else
  error_reporting(E_ALL & ~E_NOTICE);
?>
<?php
session_start();
error_reporting(0);
require_once "konmysqli.php";

$mnu = "";
if (isset($_GET["mnu"])) {
  $mnu = $_GET["mnu"];
}
if (!isset($_SESSION["cid"])) {
  die("<script>location.href='login.php';</script>");
}



$max_buku = 2;
$max_hari = 14;
$denda = 1000;
$sql = "select `denda`,`max_hari`  from `tb_setting` order by `id_setting` desc";
$ada = getJum($conn, $sql);
if ($ada > 0) {
  $d = getField($conn, $sql);
  $denda = $d["denda"];
  $max_hari = $d["max_hari"];
}
$rpdenda = $denda;
$today = date("Y-m-d");

$sql = "select * from `$tbpinjam` where status='Pinjam' order by `id_pinjam` asc";
$jum = getJum($conn, $sql);
if ($jum > 0) {
  $arr = getData($conn, $sql);
  foreach ($arr as $d) {
    $id_pinjam = $d["id_pinjam"];
    $id_anggota = $d["id_anggota"];

    $tanggal_pinjam = WKT($d["tanggal_pinjam"]);
    $jam_pinjam = $d["jam_pinjam"];
    $tanggal_selesai = WKT($d["tanggal_selesai"]);
    $selesai = $d["tanggal_selesai"];

    $sqly = "select `id_buku` from `tb_detail` where `id_pinjam`='$id_pinjam' and `status_detail`='Pinjam'";
    $jumy = getJum($conn, $sqly);
    if ($jumy > 0) {
      $dy = getField($conn, $sqly);
      $id_buku = $dy["id_buku"];
      $buku = getBuku($conn, $id_buku);
      $tgl1 = strtotime($today);
      $tgl2 = strtotime($selesai);

      $jarak = $tgl1 - $tgl2;
      $ke = ($jarak / 60 / 60 / 24) + 0;
  $SISAHARI=abs($ke);
      if ($SISAHARI <= 2 ) { //hanya -2 sd +2,
  //or $SISAHARI==14 or $SISAHARI==7
        $ke2 = "H" . $ke;
        if ($ke == 0) {
          $ke2 = "H";
        } else if ($ke > 0) {
          $ke2 = "H+" . $ke;
        }

        $sqlc = "select `idw` from `tb_wa` where id_pinjam='$id_pinjam' and `keterangan`='$ke2'";
        $jumc = getJum($conn, $sqlc);
        if ($jumc < 1) {
          $sql2 = "select `nama_anggota`,`telepon` from `$tbanggota` where `id_anggota`='$id_anggota'";
          $d2 = getField($conn, $sql2);
          $nama_anggota = $d2["nama_anggota"];
          $telepon = $d2["telepon"];
          $pesan = "Yth,$nama_anggota, Waktu peminjaman Sudah hari " . $ke2 . ", Buku $buku Harap dikembalikan, (Deadline $tanggal_selesai)";
          sentWA($conn, $id_pinjam, $id_anggota, $telepon, $pesan, $ke2);
          kirimWA($telepon, $pesan);
        }
      } //ke
    } //jk ada buku dipinjam
  } //foreach
} //Pinjam



//CEK 24 JAM
$today=date("Y-m-d");
 
$gab="";
$nom=0;
$sql = "select id_pinjam,id_anggota,tanggal_pinjam,jam_pinjam from `$tbpinjam` where status='Pesan' order by `id_pinjam` asc";
$jum = getJum($conn, $sql);
if ($jum > 0) {
  $arr = getData($conn, $sql);
  foreach ($arr as $d) {
    $id_pinjam = $d["id_pinjam"];
    $id_anggota = $d["id_anggota"];

    $tanggal_pinjam = ($d["tanggal_pinjam"]);
    $jam_pinjam = $d["jam_pinjam"]; 

$bookingDate = "$tanggal_pinjam $jam_pinjam";
$sudah24=is24HoursPassed($bookingDate); 
if($sudah24==1){
$sqlv = "select nama_anggota,telepon from `$tbanggota` where `id_anggota`='$id_anggota'";
$dv = getField($conn, $sqlv); 
$nama_anggota = $dv["nama_anggota"]; 
$telepon = $dv["telepon"];
$sqlUP = "Update `$tbpinjam` set status='Batal' where `id_pinjam`='$id_pinjam'";
process($conn, $sqlUP);
 
 
$sqly = "select `id_buku` from `tb_detail` where `id_pinjam`='$id_pinjam' and `status_detail`='Pesan'";
    $jumy = getJum($conn, $sqly);
    if ($jumy > 0) {
$sqlUP = "Update `tb_pinjam` set status='Batal',catatan='$cat' where `id_pinjam`='$id_pinjam'";
process($conn, $sqlUP);
 
$nom++; 
  $arry=getData($conn,$sqly);
  foreach($arry as $dy) {
      $id_buku = $dy["id_buku"];
  $buku = getBuku($conn, $id_buku);
  $gab.="$buku, ";
  $cat="Buku Pinjam $id_buku /$buku Gagal Pesan, Waktu Habis";
   
$sqlUP = "Update `tb_detail` set status_detail='Batal',catatan='$cat' where `id_pinjam`='$id_pinjam' and `id_buku`='$id_buku'";
process($conn, $sqlUP);
$sqlUP = "Update `tb_buku` set `stok`=`stok`+'1' where `id_buku`='$id_buku'";
process($conn, $sqlUP);
}//for
}//jum
$gab.=" $nom Buku ";
  $pesan = "Yth $nama_anggota, Waktu Masa 24 Jam Pengambilan Buku Pesan : $gab Sudah Habis ...";
          sentWA($conn, $id_pinjam, $id_anggota, $telepon, $pesan, "Expire Time");
          kirimWA($telepon, $pesan);
}//sudah24
  }//for Pesan
}//jum

//===================  
?>




Sabtu, 10 Agustus 2024

LARAVEL ELOQUENT IF


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


function pot($kalimat, $maxKata = 50) {
  $kata = explode(" ", $kalimat);

  if (count($kata) <= $maxKata) {
    return $kalimat; // Kalimat tidak perlu dipotong
  }

  $kalimatPotong = "";
  for ($i = 0; $i < $maxKata; $i++) {
    $kalimatPotong .= $kata[$i] . " ";
  }
  return $kalimat . "..."; // Tambahkan "..." di akhir kalimat
}

function getSOP($kode){
        $sql = "SELECT `nama_sop` FROM `tb_sop` WHERE `id_sop`= '" . $kode . "'";
        $results = DB::select($sql);
$hasil ="-";
if (!empty($r)) {
$hasil =  $results[0]->nama_sop;

        return $hasil;
}

function getImg($doc){
$imagePath = '/ypathfile/'.$doc;
$urlParts = asset($imagePath); 
//$gab="<a href='$urlParts' target='_blank' title='$urlParts'>$doc</a>";
return $urlParts;
}

function getLNK($nm,$doc,$v){

$imagePath = '/ypathfile/'.$doc;
$urlParts = asset($imagePath); 
//$gab="<a href='$urlParts' target='_blank' title='$urlParts'>$doc</a>";
$x="<a href=\"$urlParts\" title=\"$doc\" target=\"_blank\">".ucwords($nm)."</a>";
if($v==1){
$x="<a href=\"$urlParts\" title=\"$doc\" target=\"_blank\"><b>".ucwords($nm)."</b></a>";
}
return  $x;
}
function getList($kode){

$sql = "SELECT `judul_dokumen`,`dokumen` FROM `tb_dokumen` WHERE `id_sop`= '" . $kode . "'";
        //$results = DB::select($sql);
        //$hasil = $results[0]->nama_dosen;
$dokumen = DB::select($sql);
$gab="<div align='left'><ol>";
$i=0;
foreach ($dokumen as $dokumenItem) {
$i++;
$judul= $dokumenItem->judul_dokumen ;
$doc=$dokumenItem->dokumen;
//{{ url('ypathfile/'.$sop->flowchart)}}
//<img src="{{ asset('images/gambar.jpg') }}" alt="Gambar">
$imagePath = '/ypathfile/'.$doc;
$urlParts = asset($imagePath); 

$gab.="<li><a href='$urlParts' target='_blank' title='$urlParts'>$judul</a></li>";
}
$gab.="</ol></div>";
if($i==0){$gab="<i>Dokumen Pendukung belum tersedia....</i>";}
        return $gab;
}


 @php
    $Jpositif = 0;
    $Jnegatif = 0;
@endphp

@forelse ($datalatihs as $datalatih)
    NO: {{$loop->iteration}}<br>
    Kategori: {{$datalatih->kategori}}<hr>


    @if ($datalatih->kategori == "Positif")
        @php
            $Jpositif++;
        @endphp
    @elseif ($datalatih->kategori == "Negatif")
        @php
            $Jnegatif++;
        @endphp
    @endif

@empty
    <p>Tidak ada data.</p>
@endforelse


<p>Total Positif: {{$Jpositif}}</p>
<p>Total Negatif: {{$Jnegatif}}</p>




<?php
$as="tx".rand(1,8).".jpg";
$x=asset("images/$as");
?>
<img width="100%" height="800" src="{{ $x }}" alt="<?php echo $judul;?>">

Selisih Waktu Berjalan

Berikut adalah fungsi PHP yang menghitung selisih hari antara tanggal saat ini dengan tanggal 2024-08-22 . Jika tanggal saat ini kurang dari...