Skip to content

Instantly share code, notes, and snippets.

@herusdianto
Last active March 20, 2021 16:16
Show Gist options
  • Save herusdianto/80618e91999674acd456 to your computer and use it in GitHub Desktop.
Save herusdianto/80618e91999674acd456 to your computer and use it in GitHub Desktop.
Laravel Carbon Difference For Humans Bahasa Indonesia
<?php
// app/config/app.php
return array(
// ... konfigurasi lainnya ...
'timezone' => 'Asia/Jakarta',
'locale' => 'id',
'providers' => array(
// ... service provider lainnya ...
'Laravelrus\LocalizedCarbon\LocalizedCarbonServiceProvider',
),
'aliases' => array(
// ... alias lainnya ...
'LocalizedCarbon' => 'Laravelrus\LocalizedCarbon\LocalizedCarbon',
'DiffFormatter' => 'Laravelrus\LocalizedCarbon\DiffFactoryFacade',
),
);
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"laravel/framework": "4.2.8",
"laravelrus/localized-carbon": "1.3.3"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
],
"psr-4": {
"Acme\\": "app/Acme"
}
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
<?php
// app/start/global.php
// ... isi file global lainnya ...
DiffFormatter::extend('id', 'Acme\\DiffFormatters\\IdDiffFormatter');
<?php namespace Acme\DiffFormatters;
// app/Acme/DiffFormatters/IdDiffFormatter.php
use Laravelrus\LocalizedCarbon\DiffFormatters\DiffFormatterInterface;
use Lang;
class IdDiffFormatter implements DiffFormatterInterface {
/**
* format diffForHumans
*
* @param bool $isNow
* @param bool $isFuture
* @param int $delta
* @param int $unit
* @return string
*/
public function format($isNow, $isFuture, $delta, $unit)
{
$local = Lang::choice("units.".$unit, $delta, array(), 'id');
$text = $delta.' '.$local;
if ($isNow)
{
$text .= ($isFuture) ? ' dari sekarang' : ' yang lalu';
return $text;
}
$text .= ($isFuture) ? ' setelah' : ' sebelum';
return $text;
}
}
<?php
// app/lang/id/units.php
return [
'second' => 'detik',
'minute' => 'menit',
'hour' => 'jam',
'day' => 'hari',
'week' => 'minggu',
'month' => 'bulan',
'year' => 'tahun',
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment