Skip to content

Commit a178f17

Browse files
committed
add more function
1 parent 5d07571 commit a178f17

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Laravel Helpers
22

3-
Collection of helper functions for laravel.
3+
Collection of (9) helper functions for laravel.
44

55
## Installation
66

@@ -16,13 +16,26 @@ composer require yudhatp/laravel-helpers
1616
use Yudhatp\Helpers\Helpers;
1717
Helpers::terbilang("2000"); //Dua Ribu
1818
Helpers::indonesianMonthName("2022-11-28"); //28 November 2022
19-
Helpers::indonesianShortMonthName("2022-11-28"); //28 Nov 2022
19+
Helpers::indonesianShortMonthName("2022-11-28"); //28 Nov 22
20+
Helpers::getAgeIndonesian("1945-08-17"); //77 Tahun, 3 Bulan, 12 Hari
21+
Helpers::isWeekend("2022-11-29"); //false
22+
Helpers::generatePassword(8); //EPp218kR
23+
Helpers::indonesianFormatDecimal("2.100,00"); //2100.00
24+
Helpers::addDays("2022-11-28",2); //2022-11-30
25+
Helpers::indonesianPoliceNumberformat("B123XYZ"); //B 123 XYZ
26+
27+
//blade
28+
{{ (new \Yudhatp\Helpers\Helpers)->indonesianMonthName("2022-11-28") }}
2029
```
2130

2231
## Changelog
2332

2433
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
2534

35+
## Credits
36+
37+
- [@matriphe](https://https://gist.github.com/matriphe/3103ec578ec556bad5047b378520f070)
38+
2639

2740
## License
2841

src/Helpers.php

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,65 @@
22

33
namespace Yudhatp\Helpers;
44

5+
use Carbon\Carbon;
6+
use DateTime;
7+
58
class Helpers
69
{
10+
11+
//source from https://https://gist.github.com/matriphe/3103ec578ec556bad5047b378520f070
12+
public static function indonesianPoliceNumberformat($string)
13+
{
14+
$string = strtoupper(trim($string));
15+
16+
$pattern = '/^([A-Z]{1,3})(\s|-)*([1-9][0-9]{0,3})(\s|-)*([A-Z]{0,3}|[1-9][0-9]{1,2})$/i';
17+
if (preg_match($pattern, $string)) {
18+
return trim(strtoupper(preg_replace($pattern, '$1 $3 $5', $string)));
19+
}
20+
21+
// militer dan kepolisian
22+
$pattern = '/^([0-9]{1,5})(\s|-)*([0-9]{2}|[IVX]{1,5})*/';
23+
if (preg_match($pattern, $string)) {
24+
return trim(strtoupper(preg_replace($pattern, '$1-$3', $string)));
25+
}
26+
27+
return null;
28+
}
29+
30+
public static function addDays($date, $day){
31+
return Carbon::createFromFormat('Y-m-d',$date)->addDays($day)->toDateString();
32+
}
33+
34+
public static function indonesianFormatDecimal($number){
35+
$temp = str_replace('.','',$number);
36+
$temp = str_replace(',','.',$temp);
37+
return $temp;
38+
}
39+
40+
public static function generatePassword($char) {
41+
$tmp = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcefghijklmnopqrstuvwxyz';
42+
return substr(str_shuffle($tmp), 0, $char);
43+
}
44+
45+
public static function isWeekend($date){
46+
$day = date('D', strtotime($date));
47+
if($day == "Sat" || $day == "Sun"){
48+
return true;
49+
}else{
50+
return false;
51+
}
52+
}
53+
54+
public static function getAgeIndonesian($birthdate){
55+
$tmp = new DateTime($birthdate);
56+
$curdate = new DateTime();
57+
$diff = $tmp->diff($curdate);
58+
$year = $diff->y;
59+
$month = $diff->m;
60+
$day = $diff->d;
61+
return $year." Tahun, ".$month." Bulan, ".$day." Hari";
62+
}
63+
764
public static function indonesianMonthName($date) {
865
$date = date_create($date);
966
$month = date_format($date,"n");
@@ -29,23 +86,23 @@ public static function terbilang($nilai) {
2986
if ($nilai < 12) {
3087
$temp = " ". $huruf[$nilai];
3188
} else if ($nilai <20) {
32-
$temp = $this->terbilang($nilai - 10). " Belas";
89+
$temp = static::terbilang($nilai - 10). " Belas";
3390
} else if ($nilai < 100) {
34-
$temp = $this->terbilang($nilai/10)." Puluh". $this->terbilang($nilai % 10);
91+
$temp = static::terbilang($nilai/10)." Puluh". static::terbilang($nilai % 10);
3592
} else if ($nilai < 200) {
36-
$temp = " Seratus" . $this->terbilang($nilai - 100);
93+
$temp = " Seratus" . static::terbilang($nilai - 100);
3794
} else if ($nilai < 1000) {
38-
$temp = $this->terbilang($nilai/100) . " Ratus" . $this->terbilang($nilai % 100);
95+
$temp = static::terbilang($nilai/100) . " Ratus" . static::terbilang($nilai % 100);
3996
} else if ($nilai < 2000) {
40-
$temp = " Seribu" . $this->terbilang($nilai - 1000);
97+
$temp = " Seribu" . static::terbilang($nilai - 1000);
4198
} else if ($nilai < 1000000) {
42-
$temp = $this->terbilang($nilai/1000) . " Ribu" . $this->terbilang($nilai % 1000);
99+
$temp = static::terbilang($nilai/1000) . " Ribu" . static::terbilang($nilai % 1000);
43100
} else if ($nilai < 1000000000) {
44-
$temp = $this->terbilang($nilai/1000000) . " Juta" . $this->terbilang($nilai % 1000000);
101+
$temp = static::terbilang($nilai/1000000) . " Juta" . static::terbilang($nilai % 1000000);
45102
} else if ($nilai < 1000000000000) {
46-
$temp = $this->terbilang($nilai/1000000000) . " Milyar" . $this->terbilang(fmod($nilai,1000000000));
103+
$temp = static::terbilang($nilai/1000000000) . " Milyar" . static::terbilang(fmod($nilai,1000000000));
47104
} else if ($nilai < 1000000000000000) {
48-
$temp = $this->terbilang($nilai/1000000000000) . " Trilyun" . $this->terbilang(fmod($nilai,1000000000000));
105+
$temp = static::terbilang($nilai/1000000000000) . " Trilyun" . static::terbilang(fmod($nilai,1000000000000));
49106
}
50107
return $temp;
51108
}

0 commit comments

Comments
 (0)