<?php
$tahun = date('y');
  $bulan = date('m');
  $hari = date('d');
  $rand = rand(1, 9999);
  $char = "N";
  $newID = sprintf($tahun).sprintf($bulan).sprintf($hari).sprintf("%04s", $rand);

  $cek = $newID;
  $sub_cek = substr($cek,6,2);

  while ($sub_cek == "77"){
    $rand = rand(1, 9999);
  $char = "N";
  $newID = sprintf($tahun).sprintf($bulan).sprintf($hari).sprintf("%04s", $rand);

  $cek = $newID;
  $sub_cek = substr($cek,6,2);
  }

  $cekid=mysqli_num_rows(mysqli_query($conn, "SELECT * FROM santri WHERE id_santri='$newID'"));
  ?>