Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * IndonesiaDateTime.
- *
- * Extends Class DateTime agar memberikan format penamaan hari dan bulan dalam bahasa Indonesia.
- *
- * @author: IjorTengab
- * @last_update: 2019 05 11
- *
- * Latar Belakang:
- * Berawal dari builtin PHP Class `DateTime` yang ~tidak bisa~ susah untuk
- * di-extends agar dapat di-override method `format`, maka Class ini menjadi
- * alternative solusi.
- *
- * Cara Penggunaan:
- * Penggunaan class ini sama persis seperti menggunakan Class `DateTime`.
- * Hanya jika ingin menggunakan comparison, maka gunakan method `get`.
- *
- * Contoh 1, Print Format:
- * ```
- * $a = new DateTime;
- * $x = new IndonesiaDateTime;
- * echo $a->format('l'); // return Saturday
- * echo $x->format('l'); // return Sabtu
- * ```
- *
- * Contoh 2, Comparison:
- * ```
- * $a = new DateTime('yesterday');
- * $b = new DateTime('tomorrow');
- * var_dump($a < $b); // return true
- *
- * $x = new IndonesiaDateTime('yesterday');
- * $y = new IndonesiaDateTime('tomorrow');
- * var_dump($x->get() < $y->get()); // return true
- *
- * $a = new DateTime('yesterday');
- * $x = new IndonesiaDateTime('tomorrow');
- * var_dump($a < $x->get()); // return true
- * ```
- *
- * Contoh 3, Minggu dan Ahad:
- * ```
- * $x = new IndonesiaDateTime('this Sunday');
- * echo $x->format('l'); // Minggu
- * $x->ahadSwitch(true);
- * echo $x->format('l'); // Ahad
- * $x->ahadSwitch(false);
- * echo $x->format('l'); // Minggu
- *
- * $y = new IndonesiaDateTime('this Sunday');
- * echo $y->format('D'); // Min
- * $y->ahadSwitch(true);
- * echo $y->format('D'); // Aha
- * $y->ahadSwitch(false);
- * echo $y->format('D'); // Min
- * ```
- */
- class IndonesiaDateTime
- {
- /**
- * Property sebagai penanda menggunakan diksi Ahad alih-alih Minggu.
- */
- protected $ahad_switch = false;
- /**
- * Property penyimpanan Object DateTime.
- */
- protected $date;
- /**
- * Convert format dengan menutupi tiap placeholder dengan kurung kotak.
- * Berguna untuk mencegah salah translate antara Mon dan Monday yang
- * mengandung substring `Mon`.
- */
- protected $converter = [
- 'D' => '[D]',
- 'l' => '[l]',
- 'F' => '[F]',
- 'M' => '[M]',
- ];
- /**
- * Translate kata.
- */
- protected $replacement = [
- '[Sun]' => 'Min',
- '[Mon]' => 'Sen',
- '[Tue]' => 'Sel',
- '[Wed]' => 'Rab',
- '[Thu]' => 'Kam',
- '[Fri]' => 'Jum',
- '[Sat]' => 'Sab',
- '[Sunday]' => 'Minggu',
- '[Monday]' => 'Senin',
- '[Tuesday]' => 'Selasa',
- '[Wednesday]' => 'Rabu',
- '[Thursday]' => 'Kamis',
- '[Friday]' => 'Jumat',
- '[Saturday]' => 'Sabtu',
- '[Jan]' => 'Jan',
- '[Feb]' => 'Feb',
- '[Mar]' => 'Mar',
- '[Apr]' => 'Apr',
- '[May]' => 'Mei',
- '[Jun]' => 'Jun',
- '[Jul]' => 'Jul',
- '[Aug]' => 'Agu',
- '[Sep]' => 'Sep',
- '[Oct]' => 'Okt',
- '[Nov]' => 'Nov',
- '[Dec]' => 'Des',
- '[January]' => 'Januari',
- '[February]' => 'Februari',
- '[March]' => 'Maret',
- '[April]' => 'April',
- '[May]' => 'Mei',
- '[June]' => 'Juni',
- '[July]' => 'Juli',
- '[August]' => 'Agustus',
- '[September]' => 'September',
- '[October]' => 'Oktober',
- '[November]' => 'November',
- '[December]' => 'Desember',
- ];
- /**
- * Mengembalikan object DateTime, berguna untuk comparison.
- */
- public function get()
- {
- return $this->date;
- }
- /**
- * Fungsi untuk turn on/off pemilihan format Ahad alih-alih Minggu.
- */
- public function ahadSwitch($boolean)
- {
- return $this->ahad_switch = $boolean;
- }
- /**
- * Construct.
- */
- public function __construct()
- {
- $args = func_get_args();
- if (count($args) == 1 && $args[0] instanceOf \DateTime) {
- $this->date = $args[0];
- }
- else {
- isset($args[0]) or $args[0] = 'now';
- isset($args[1]) or $args[1] = null;
- $this->date = new \DateTime($args[0], $args[1]);
- }
- return $this;
- }
- /**
- * Magic untuk semua fungsi static DateTime.
- */
- public static function __callStatic($name, $arguments)
- {
- $date = call_user_func_array(['\DateTime', $name], $arguments);
- return new static($date);
- }
- /**
- * Magic untuk semua fungsi instance DateTime.
- */
- public function __call($name, $arguments)
- {
- $test = call_user_func_array([$this->date, $name], $arguments);
- if ($test instanceOf \DateTime) {
- $this->date = $test;
- return $this;
- }
- else {
- return $test;
- }
- }
- /**
- * Sama seperti DateTime::format. Disini akan di-translate kata dalam bahasa
- * English ke Indonesia.
- */
- public function format($format)
- {
- $format = strtr($format, $this->converter);
- $result = $this->date->format($format);
- $replacement = $this->replacement;
- if ($this->ahad_switch) {
- $replacement['[Sun]'] = 'Aha';
- $replacement['[Sunday]'] = 'Ahad';
- }
- return strtr($result, $replacement);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement