Documentation

examples

Extending Carbon Instance

Extending Carbon Instance

In order to use the toNepaliDate() and toEnglishDate() mixin on Carbon instances, you need to register the NepaliDateMixin mixin in your Laravel service provider.

You can do so by adding the following code to your AppServiceProvider

use Anuzpandey\LaravelNepaliDate\NepaliDateMixin;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Register the NepaliDateMixin
        Carbon::mixin(new NepaliDateMixin());
    }
}

Or create your own service provider with php artisan make:provider MacroableServiceProvider, register it in config/app.php and add the following code to the boot method.

use Anuzpandey\LaravelNepaliDate\NepaliDateMixin;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;

class MacroableServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Register the NepaliDateMixin
        Carbon::mixin(new NepaliDateMixin());
    }
}

Usage with Mixin

$engDate = '1996-04-22';
Carbon::parse($engDate)->toNepaliDate();
// Result: 2053-01-10

Carbon::parse($engDate)->toNepaliDate(format: 'D, j F Y');
// Result: рд╕реЛрдо, резреж рд╡реИрд╢рд╛рдЦ реирежрелрей