Laravel English To Nepali Date Converter: A Complete Guide
Published on February 15, 2024 by Dinesh Uprety
Laravel Nepali Date is a Laravel package that simplifies the conversion of dates between the Gregorian (English) and Nepali (Bikram Sambat). This package is a handy tool for projects that require handling dates in both English and Nepali formats, such as websites and applications targeting users in Nepal.
In this post, I'll teach you how to install and utilize the Laravel Nepali Date package in your Laravel projects.
Installation
You can install the package via composer:
composer require anuzpandey/laravel-nepali-date
Usage
After installation is completed, Laravel Nepali Date has two static methods: from()
and toNepaliDate()
or toEnglishDate()
. The from method takes a date string as an argument and returns a Laravel Nepali Date instance. The toNepaliDate and toEnglishDate methods take an optional format and locale arguments and return the converted date string.
For example, to convert an English date to a Nepali date, you can write:
<?php use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; $engDate = '1996-04-22';LaravelNepaliDate::from($engDate)->toNepaliDate();// Result: 2053-01-10
After that, you can easily convert English dates to Nepali dates. Not only the English date but also the Nepali date You can convert Nepali dates to English dates too. Here now, I will show
<?php use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; $nepDate = '2053-01-10';LaravelNepaliDate::from($nepDate)->toEnglishDate();// Result: 1996-04-22
You can format and locale the date too by passing the parameters toNepaliDate()
and toEnglishDate()
here:
<?php use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; //passing param toNepaliDate function$engDate = '1996-04-22';LaravelNepaliDate::from($engDate)->toNepaliDate(format: 'D, j F Y');// Result: सोम, १० वैशाख २०५३ // Format Specifiers are supported and listed belowLaravelNepaliDate::from($engDate)->toNepaliDate(format: 'D, j F Y', locale: 'en');// Result: Mon, 10 Baisakh 2053
<?php use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate; //passing param toEnglishDate function$nepDate = '2053-01-10';LaravelNepaliDate::from($nepDate)->toEnglishDate(format: 'l, jS F Y');// Result: Sunday, 22nd April 1996 // Format Specifiers are supported and listed belowLaravelNepaliDate::from($nepDate)->toEnglishDate(format: 'l, j F Y', locale: 'np');// Result: आइतबार, २२ बैशाख १९९६
Format Specifiers
The following format specifiers are supported for formatting dates:
-
Y
- Year in four digits -
y
- Year in two digits -
m
- Month in two digits with leading zero (01-12/०१-१२) -
n
- Month in one or two digits without leading zero (1-12/१-१२) -
M
- Month in three letters (Jan-Dec) -
F
- Month in full name (January-December/बैशाख-चैत्र) -
d
- Day in two digits with leading zero (01-31/०१-३२) -
j
- Day in one or two digits without leading zero (1-31/१-३२) -
D
- Day in three letters (Sun-Sat/आइत-शनि) -
l
- Day in full name (Sunday-Saturday/आइतबार-शनिबार) -
S
- Day in two letters (st, nd, rd, th)
This package offers more functions, such as a helper function to convert dates.
<?php // Convert English date to Nepali date (B.S.). toNepaliDate("1996-04-22") // Result: 2053-01-10 // Convert Nepali date to English date (A.D.). toEnglishDate("2053-01-10") // Result: 1996-04-22?>
For more details, you can read the git ReadMe file written by anuzpandey on this package Laravel Nepali Date.