Skip to content

Instantly share code, notes, and snippets.

@autotrof
Created January 14, 2021 14:53
Show Gist options
  • Save autotrof/0d1ba9ba25eaa2eb221c4b5594e6d897 to your computer and use it in GitHub Desktop.
Save autotrof/0d1ba9ba25eaa2eb221c4b5594e6d897 to your computer and use it in GitHub Desktop.
Helpers laravel untuk meringankan beban koding kalian
<?php
// 1
if (! function_exists('number_to_day')) {
function number_to_day($nomor_hari)
{
$hari = [
"",
"Ahad",
"Senin",
"Selasa",
"Rabu",
"Kamis",
"Jumat",
"Sabtu"
];
return $hari[(int)$nomor_hari];
}
}
// 2
if (! function_exists('day_to_number')) {
function day_to_number($hari)
{
$hari = strtolower($hari);
$list_hari = [
'senin'=>2,
'selasa'=>3,
'rabu'=>4,
'kamis'=>5,
'jumat'=>6,
'sabtu'=>7,
'ahad'=>1
];
return $list_hari[$hari];
}
}
// 3
if (! function_exists('number_to_month')) {
function number_to_month($nomor_bulan)
{
$bulan = [
'',
'Januari',
'Februari',
'Maret',
'April',
'Mei',
'Juni',
'Juli',
'Agustus',
'September',
'Oktober',
'November',
'Desember'
];
return $bulan[(int)$nomor_bulan];
}
}
// 4
if (! function_exists('create_date_range')) {
function create_date_range($startDate, $endDate, $format = "Y-m-d", $step=1)
{
$endDate = date($format,strtotime($endDate.' +1 day'));
$periode = new DatePeriod(
new DateTime($startDate),
new DateInterval('P'.$step.'D'),
new DateTime($endDate)
);
$list_tanggal = [];
foreach ($periode as $key => $value) array_push($list_tanggal,$value->format($format));
return $list_tanggal;
}
}
// 5
if (! function_exists('db')) {
function db($table,$connection=null)
{
if(is_null($connection)) $connection = env('DB_CONNECTION');
if($table!=null)
return DB::connection($connection)->table($table);
else
return DB::connection($connection);
}
}
// 6
if(! function_exists('null_to_zero')){
function null_to_zero($var) {
if(is_array($var)){
foreach ($var as $key => $value) {
$var[$key] = $value!==null?$value:0;
}
return $var;
}else{
return $var!=null?$var:0;
}
}
}
// 7
if(! function_exists('jarak_bulan')){
function jarak_bulan($start, $end)
{
$tahun_start = (int)date('Y',strtotime($start));
$bulan_start = (int)date('m',strtotime($start));
$tahun_end = (int)date('Y',strtotime($end));
$bulan_end = (int)date('m',strtotime($end));
if($tahun_start!=$tahun_end){
return 12-$bulan_start+$bulan_end;
}
return $bulan_end - $bulan_start;
}
}
// 8
if(!function_exists('u')){
function u($param='')
{
return env('HTTPS')?secure_url($param):url($param);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment