rohanadhikari / nepali-date
A PHP package for handling Nepali Date and Time (Bikram Sambat - BS), with AD/BS conversion, fiscal years and quarters, formatting, and Laravel support.
Fund package maintenance!
Requires
- php: ^8.1
Requires (Dev)
- laravel/pint: ^1.25
- pestphp/pest: ^3.8
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
NepaliDate is a dependency-free PHP library for working with the Nepali calendar (Bikram Sambat / BS) — the calendar of Nepal. It converts between AD (Gregorian) and BS, formats, parses, compares, and manipulates Nepali dates with a fluent, Carbon-inspired API, and ships first-class Laravel support (Eloquent casts, validation rules, and Blade directives).
Whether you're building a Nepali government portal, a Nepal-based e-commerce app, a school/attendance system, or just need accurate AD ⇄ BS date conversion in PHP, NepaliDate gives you a complete, tested toolkit instead of hand-rolled conversion math.
✨ Features
- 🕒 Full Nepali (BS) date and time support — not just year/month/day conversion
- 🗓 Simple, accurate conversion between AD and BS, optimized for bulk conversion — see Performance
- 🔁 Both mutable (
NepaliDate) and immutable (NepaliDateImmutable) variants - ➕ Add, subtract, and snap dates by year, month, day, hour, minute, or second
- 💰 Nepali fiscal years, quarters (त्रैमासिक) and chaumasik (चौमासिक) periods — see Fiscal Year
- 🌐 Built-in Nepali (
np) and English (en) locales, with full customization support - 🔢 Format dates with
date()-style tokens, or use predefined format constants - 🔍 Rich comparison API —
isToday(),isWeekend(),between(), and more - 🧩 Laravel-ready: Eloquent casts, form validation rules, and Blade directives out of the box
- 🔤 Standalone
NepaliNumbersutility for digit, currency, and word conversion
📦 Installation
Install via Composer:
composer require rohanadhikari/nepali-date
No other setup is required for plain PHP. In Laravel, the service provider is auto-discovered — see Laravel Integration.
🚀 Quick Start
use RohanAdhikari\NepaliDate\NepaliDate; use RohanAdhikari\NepaliDate\NepaliDateImmutable; // Current Nepali date and time $now = NepaliDate::now(); // Parse a natural-language notation and manipulate it $date = NepaliDate::fromNotation('tomorrow'); $date->addDays(5); // Convert to Gregorian (AD) $adDate = $date->toAd(); // Format it echo $date->format(NepaliDate::FORMAT_DATETIME_24_FULL); // e.g. 2082-06-30 14:45:22 // Immutable variant — every mutation returns a new instance $immutableNow = NepaliDateImmutable::now(); $newDate = $immutableNow->addDays(10); // original is unchanged
Note
Predefined format constants are listed in Constants.
Using in plain PHP (no framework)
require __DIR__ . '/vendor/autoload.php'; use RohanAdhikari\NepaliDate\NepaliDate; $now = NepaliDate::now(); echo $now->format(NepaliDate::FORMAT_DATETIME_24_FULL); $ad = new DateTime('now', new DateTimeZone('Asia/Kathmandu')); echo NepaliDate::fromAd($ad)->format(NepaliDate::FORMAT_DATE_YMD);
Make sure the path to vendor/autoload.php is correct relative to your script — Composer is the recommended way to load the package.
📖 Documentation
The core README covers the essentials; everything else lives in docs/, organized by topic (Carbon-style) so you only read what you need:
| Guide | Covers |
|---|---|
| Initialization | now(), fromAd(), fromNotation(), fromTimestamp(), direct construction |
| Formatting | Format tokens, examples, locale-aware output |
| Parsing | parse(), createFromFormat(), custom parse patterns |
| Arithmetic & Shifting | Add/subtract units, month overflow control, diff, timezone/week shifting |
| Getters | Year, time, day, month, timezone, and locale-based accessors |
| Setters | Basic, unit-based, and combined setters |
| Boundaries | startOf()/endOf() for day, week, month, quarter, year, decade... |
| Fiscal Year | Nepali fiscal years, quarters, chaumasiks, and Laravel queries |
| Comparison | eq(), between(), isToday(), isWeekend(), and other state checks |
| Constants | Weekday, month, locale, and format constants |
| Locale Customization | Customizing month/weekday names per locale |
| Macros | Adding custom methods at runtime |
| Nepali Numbers | Digit, currency, and word conversion utilities |
| Calendar | Low-level BS/AD calendar math and lookup tables |
| Exceptions | What can be thrown, and how to guard against it |
| Laravel Integration | Eloquent casts, validation rules, Blade directives |
| Performance | Bulk-conversion results and practical guidance for high-throughput usage |
🌐 Locale
Available locales: en and np.
$date = NepaliDate::now(); $date->setLocale(NepaliDate::NEPALI); // or ->locale('np') echo $date->format(NepaliDate::FORMAT_DATETIME_24); // Example Output: २०८२-०६-३१ २१:३४ $date->resetLocale();
Note
Set a global default for every new instance with NepaliDate::defaultLocale(NepaliDate::NEPALI). See Locale Customization to change the underlying month/weekday names.
Nepali Fiscal Year
use RohanAdhikari\NepaliDate\Fiscal\FiscalYear; $fy = FiscalYear::parse('2083/84'); $fy->start()->format('Y-m-d'); // 2083-04-01 $fy->quarter(2)->start()->format('Y-m-d'); // 2083-07-01 (new NepaliDate(2083, 8, 13))->getFiscalQuarter(); // 2
See the Fiscal Year guide.
Nepali Date in Laravel
Using Nepali date in Laravel is a first-class use case for this package: Eloquent casts, form validation rules, and Blade directives (@nepaliDate, @nepaliNow, @nepaliWeekend, ...) are all included and auto-registered via a Laravel service provider — no extra config. See the Laravel Integration Guide (with FAQ) for the full reference.
use RohanAdhikari\NepaliDate\Laravel\Casts\ADAsNepaliDate; class Post extends Model { protected function casts(): array { return ['published_at' => ADAsNepaliDate::class]; } }
<p>Published: @nepaliDate($post->published_at, 'l, F j, Y')</p>
🧪 Testing
composer test # Run the Pest test suite composer analyse # Run PHPStan static analysis composer format # Run Laravel Pint composer time:check # Benchmark AD <-> BS conversion — see docs/PERFORMANCE.md
🤝 Contributing
Contributions are welcome and will be fully credited. Please read the Contributing Guide before opening an issue or pull request.
🔒 Security
If you discover a security vulnerability, please see SECURITY.md for responsible disclosure instructions instead of using the public issue tracker.
📄 License
The MIT License (MIT). See LICENSE.md for details.