Last active
November 14, 2020 13:00
-
-
Save ijortengab/7db2883e7d9eafde5421a231e207ae87 to your computer and use it in GitHub Desktop.
Extends Class DateTime agar memberikan format penamaan hari dan bulan dalam bahasa Indonesia.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* IndonesiaDateTime. | |
* | |
* Extends Class DateTime agar memberikan format penamaan hari dan bulan dalam | |
* bahasa Indonesia. | |
* | |
* @author: IjorTengab | |
* @last_update: 2020 11 14 | |
* | |
* 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 < $y); // return true | |
* | |
* $a = new DateTime('yesterday'); | |
* $x = new IndonesiaDateTime('tomorrow'); | |
* var_dump($a < $x); // 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 extends \DateTime | |
{ | |
/** | |
* Property sebagai penanda menggunakan diksi Ahad alih-alih Minggu. | |
*/ | |
protected $ahad_switch = false; | |
/** | |
* 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', | |
]; | |
/** | |
* Fungsi untuk turn on/off pemilihan format Ahad alih-alih Minggu. | |
*/ | |
public function ahadSwitch($boolean) | |
{ | |
return $this->ahad_switch = $boolean; | |
} | |
/** | |
* Sama seperti DateTime::format. Disini akan di-translate kata dalam bahasa | |
* English ke Indonesia. | |
*/ | |
public function format($format) | |
{ | |
$format = strtr($format, $this->converter); | |
$result = parent::format($format); | |
$replacement = $this->replacement; | |
if ($this->ahad_switch) { | |
$replacement['[Sun]'] = 'Aha'; | |
$replacement['[Sunday]'] = 'Ahad'; | |
} | |
return strtr($result, $replacement); | |
} | |
public static function createFromFormat($format, $datetime, $timezone = null) { | |
return new static((\DateTime::createFromFormat($format, $datetime, $timezone))->format('c')); | |
} | |
} |
Author
ijortengab
commented
May 11, 2019
•
Command line version.
<?php
require "IndonesiaDateTime.php";
// Variable.
$year = isset($_SERVER['argv']['1']) ? $_SERVER['argv']['1'] : date('Y');
// Process.
$yearCurrent = IndonesiaDateTime::createFromFormat('Y-m-d', $year.'-01-01');
$yearCurrent->modify('midnight');
$yearCurrent->ahadSwitch(true);
$yearNext = IndonesiaDateTime::createFromFormat('Y-m-d', ($year + 1) .'-01-01');
$yearNext->modify('midnight');
while ($yearCurrent < $yearNext) {
echo $yearCurrent->format('Ymd l:');
echo PHP_EOL;
$yearCurrent->modify('+1 day');
// break;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment