Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $sdm = ['Budi','Dora','Dewi','Indro','Dono'];
- $day = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
- $hari = ['Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Ahad'];
- $month_list = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
- $slot_jadwal = ['Pagi','Siang','Sore'];
- $no_go = ['Rabu Sore','Kamis Sore'];
- $no_schedule = [
- 'Budi' => ['Senin','Rabu Siang','Jumat Siang'],
- 'Dora' => ['Rabu Sore', 'Kamis Sore'],
- 'Dewi' => ['Rabu Sore','Kamis Pagi','Ahad'],
- 'Indro' => ['Selasa Sore','Rabu Sore','Kamis Sore','Sabtu'],
- 'Dono' => ['Senin','Selasa','Rabu Sore','Kamis Sore','Jumat'],
- ];
- $no_area = [
- 'Dewi' => ['Jakarta Barat','noll']
- ];
- $area = ['Jakarta Barat','Pondok Aren','Ciputat','Pondok Labu','Kebayoran Lama'];
- $area_detals = ['Jakarta Barat' => ['Grogol','Tanah Abang','Thamrin City'],
- 'Pondok Aren' => ['Ceger','Japos','Pondok Kacang'],
- 'Ciputat' => ['UIN','Kasino 1','Kasino 2','ZXC'],
- 'Pondok Labu' => ['ATK 1','ATK 2','Tex 1','Tex 2','MMM','Gandul 1','Gandul 2'],
- 'Kebayoran Lama' => ['ATK 1','ATK 2','ATK 3','Tex 1','Tex 2','Tex 3','Sepatu'],
- ];
- $tahun = 2018;
- $bulan = 1;
- //===================
- ApplyHeader($tahun);
- $booked = [];
- $twice = [];
- $kemarin= [];
- $ctrKemarin = 0;
- while ($bulan < 13) {
- echo "<h1>Bulan : ".$month_list[$bulan]."</h1>\n\n";
- $tanggal = 1;
- // bikin header table bulan
- echo "
- <table class='greenTable'>
- <thead>
- <tr>
- <th>Senin</th>
- <th>Selasa</th>
- <th>Rabu</th>
- <th>Kamis</th>
- <th>Jumat</th>
- <th>Sabtu</th>
- <th>Ahad</th>
- </tr>
- </thead>
- <tbody>
- <tr>";
- $hari_ini = getDay($tanggal,$bulan,$tahun);
- // jika tanggal 1, bikin kolom-kolom kosong s/d ke kolom hari yang benar
- // hitung jumlah kolom yang perlu dikosongkan
- $skip = array_search($hari_ini, $hari);
- // kosongkan kolom
- for ($i = 1; $i <= $skip; $i++) {
- echo "<td></td>";
- };
- // pilih salah satu hari Sabtu
- $xSaturday = getRandomSaturday($bulan, $tahun);
- // pilih salah satu hari Ahad
- $xSunday = getRandomSunday($bulan, $tahun);
- // mulai buat daftar keliling bulan ini
- while (checkdate ($bulan, $tanggal, $tahun)) {
- $hari_ini = getDay($tanggal,$bulan,$tahun);
- // jika hari Senin, bikin row baru
- if ($hari_ini == 'Senin') {
- echo "</tr><tr>";
- };
- // kalau sabtu / minggu = skip
- if ( (($hari_ini == 'Sabtu') || ($hari_ini == 'Ahad')) && ($tanggal <> $xSunday) && ($tanggal <> $xSaturday) ) {
- // do nothing
- echo "<td>$tanggal</td>";
- } else {
- // bikin kolom hari
- echo "<td><h3>".$hari_ini." : ".$tanggal."</h3><br />\n\n";
- foreach ($area as &$daerah) {
- // dapatkan waktu
- do {
- $x = getRandomNumber(0, sizeof($slot_jadwal)-1);
- $slot = $slot_jadwal[$x];
- $slot_hari = $hari_ini." ".$slot;
- } while ( in_array($slot_hari, $no_go) );
- // dapatkan personil
- $ctr = 1;
- $dapat_sdm = false;
- do {
- $nomor = getRandomNumber(0, (sizeof($sdm)-1));
- $nama = $sdm[$nomor];
- $ctr++;
- // jika $ctr > 10000, berarti ada infinite loop
- // pecahkan dengan cara sbb :
- if ($ctr > 1000) {
- $nama = '';
- // temukan orang yang belum ada di $booked[]
- foreach ($sdm as &$n) { //echo "nama1 ".$n."/".$slot_hari;
- if (!(in_array($n, $booked)) && !(AdaKonflikJadwal($n, $slot_hari, $no_schedule)) && !(in_array($daerah, $no_area["$n"])) && !(SudahKemarin($n,$daerah,$kemarin)) ) {
- $nama = $n;
- };
- };
- if ($nama <> '') {
- $booked[] = $nama; if ($nama == 'Devy') { $twice[] = $nama;};
- // jika tidak ada, maka ambil dari $booked[]
- } elseif ($nama == '') {
- do {
- $nomor = getRandomNumber(0, (sizeof($booked)-1));
- $nama = $booked[$nomor];
- } while ( (in_array($nama, $twice)) && SudahKemarin($nama,$daerah,$kemarin) );
- $twice[] = $nama;
- }; // if ($nama == '')
- $dapat_sdm = true;
- }; // if ($ctr > 10000)
- if ($dapat_sdm) { break;};
- } while ( (AdaKonflikJadwal($nama, $slot_hari, $no_schedule)) || (in_array($nama, $booked)) || (in_array($daerah, $no_area["$nama"])) || (in_array($nama, $twice)) || SudahKemarin($nama,$daerah,$kemarin) );
- $booked[] = $nama; if ($nama == 'Devy') { $twice[] = $nama;};
- $kemarin[$ctrKemarin] = [$nama,$daerah];
- $ctrKemarin++;
- // cetak nama & jadwal
- //echo $daerah." : ".$nama." - ".$slot."\n\n";
- echo $daerah." : ".$nama."<br />\n\n";
- }; // foreach ($area as &$daerah)
- // tutup kolom hari
- echo "</td>";
- }; // if (($hari == 'Sabtu') || $hari == 'Ahad')
- $booked = [];
- $twice = [];
- $tanggal++;
- if ( $ctrKemarin > (sizeof($sdm)+1) ) { $ctrKemarin = 0;};
- }; // while (checkdate ($bulan, $tanggal, $tahun)) {
- $bulan++;
- // bikin footer table bulan
- echo "</tr></tbody></table>";
- }; // while (bulan < 13)
- exit(); // end
- //===================
- function getRandomSunday($month, $year) {
- $month_list = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
- $sunday_list = [];
- $today = 1;
- // get a list of Sundays
- while (checkdate($month, $today, $year)) {
- $curdate = strtotime($today.' '.$month_list[$month].' '.$year);
- if (date('l', $curdate) == 'Sunday') {
- $sunday_list[] = $today.'-'.$month_list[$month].'-'.$year;
- };
- $today++;
- };
- // pick a random Sunday
- $max = sizeof($sunday_list) - 1;
- $result = getRandomNumber(0, $max);
- return $sunday_list[$result];
- };
- function getRandomSaturday($month, $year) {
- $month_list = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
- $sunday_list = [];
- $today = 1;
- // get a list of Saturday
- while (checkdate($month, $today, $year)) {
- $curdate = strtotime($today.' '.$month_list[$month].' '.$year);
- if (date('l', $curdate) == 'Saturday') {
- $sunday_list[] = $today.'-'.$month_list[$month].'-'.$year;
- };
- $today++;
- };
- // pick a random Sunday
- $max = sizeof($sunday_list) - 1;
- $result = getRandomNumber(0, $max);
- return $sunday_list[$result];
- };
- function getRandomNumber ($min, $max) {
- // better random seed
- $x = openssl_random_pseudo_bytes(100);
- $x = hash('crc32', $x);
- mt_srand(intval(hexdec($x)));
- $result = mt_rand($min,$max);
- //$result = random_int($min,$max);
- //$src = "https://www.random.org/integers/?num=1&min=".$min."&max=".$max."&col=1&base=10&format=plain&rnd=new"; $dh = fopen("$src",'r'); $result = fread($dh,1);
- return $result;
- }
- function getDay($tanggal, $bulan, $tahun) {
- $month_list = ['0','January','February','March','April','May','June','July','August','September','October','November','December'];
- $day = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];
- $hari = ['Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Ahad'];
- // dapatkan hari
- $hari_ini = date('l', strtotime($tanggal.' '.$month_list[$bulan].' '.$tahun));
- // ubah jadi bahasa Indonesia
- $pos = array_search($hari_ini, $day);
- $hari_ini = $hari[$pos];
- return $hari_ini;
- }
- function AdaKonflikJadwal ($nama, $slot_hari, $no_schedule) {
- $oke = false;
- foreach ($no_schedule[$nama] as &$jadwal) {
- if (strpos($slot_hari, $jadwal) !== false) {
- $oke = true;
- //echo "NIH : ".$slot_hari."/".$jadwal;
- };
- };
- return $oke;
- }
- function SudahKemarin($nama,$daerah,$kemarin) {
- $sudah = false;
- foreach ($kemarin as &$v) {
- if ( ($v[0] == $nama) && ($v[1] == $daerah) ) {
- $sudah = true;
- };
- };
- return $sudah;
- }
- function ApplyHeader($tahun) {
- ?>
- <html>
- <body>
- <style>
- table.greenTable {
- font-family: Arial, Helvetica, sans-serif;
- border: 6px solid #24943A;
- background-color: #D4EED1;
- width: 100%;
- text-align: center;
- }
- table.greenTable td, table.greenTable th {
- border: 1px solid #24943A;
- padding: 3px 2px;
- }
- table.greenTable tbody td {
- font-size: 13px;
- }
- table.greenTable thead {
- background: #24943A;
- background: -moz-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
- background: -webkit-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
- background: linear-gradient(to bottom, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
- border-bottom: 0px solid #444444;
- }
- table.greenTable thead th {
- font-size: 19px;
- font-weight: bold;
- color: #F0F0F0;
- text-align: center;
- border-left: 2px solid #24943A;
- }
- table.greenTable thead th:first-child {
- border-left: none;
- }
- table.greenTable tfoot {
- font-size: 13px;
- font-weight: bold;
- color: #F0F0F0;
- background: #24943A;
- background: -moz-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
- background: -webkit-linear-gradient(top, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
- background: linear-gradient(to bottom, #5baf6b 0%, #3a9e4d 66%, #24943A 100%);
- border-top: 1px solid #24943A;
- }
- table.greenTable tfoot td {
- font-size: 13px;
- }
- table.greenTable tfoot .links {
- text-align: right;
- }
- table.greenTable tfoot .links a{
- display: inline-block;
- background: #FFFFFF;
- color: #24943A;
- padding: 2px 8px;
- border-radius: 5px;
- }
- </style>
- <h1><center>Jadwal Keliling tahun <?php echo $tahun; ?></center></h1>
- <?php
- }
- function ApplyFooter() {
- ?>
- </table>
- </body>
- </html>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement