├── src
├── Traits
│ ├── IsLeapYearTrait.php
│ ├── DiffForHumsTrait.php
│ ├── HelperTrait.php
│ ├── EnglishDateTrait.php
│ ├── CalendarDateDataTrait.php
│ └── NepaliDateTrait.php
├── Enums
│ └── NepaliMonth.php
├── LaravelNepaliDateServiceProvider.php
├── Mixin
│ └── NepaliDateMixin.bak
├── LaravelNepaliDate.php
├── DataTransferObject
│ └── NepaliDateArrayData.php
├── Directives
│ └── NepaliDateDirective.php
├── Helper
│ └── helper.php
└── Constants
│ └── NepaliDate.php
├── LICENSE.md
├── config
└── nepali-date.php
├── composer.json
├── CONTRIBUTING.md
└── README.md
/src/Traits/IsLeapYearTrait.php:
--------------------------------------------------------------------------------
1 | convertEnToNpNumber($numberData).$this->convertEnToNpWord($remainingData);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/LaravelNepaliDateServiceProvider.php:
--------------------------------------------------------------------------------
1 | name('laravel-nepali-date')
14 | ->hasConfigFile();
15 | }
16 |
17 | public function boot(): void
18 | {
19 | parent::boot();
20 | NepaliDateDirective::register();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Mixin/NepaliDateMixin.bak:
--------------------------------------------------------------------------------
1 | toDateString();
18 |
19 | return LaravelNepaliDate::from($date)->toNepaliDate($format, $locale);
20 | };
21 | }
22 |
23 | public function toEnglishDate(): Closure
24 | {
25 | return function (?string $format = 'Y-m-d', ?string $locale = 'en') {
26 | $date = $this->toDateString();
27 |
28 | return LaravelNepaliDate::from($date)->toEnglishDate($format, $locale);
29 | };
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/LaravelNepaliDate.php:
--------------------------------------------------------------------------------
1 | explode('-')->toArray();
32 |
33 | return new static((int) $year, (int) $month, (int) $day);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) anuzpandey
The format parameter is used to specify the desired format of the Nepali date.
11 | * @param string|null $localeThe "locale" parameter is used to specify the language and region. Supported languages are en and np
12 | * @return stringNepali date converted from the given English Date.
13 | */ 14 | function toNepaliDate(string $date, ?string $format = null, ?string $locale = null): string 15 | { 16 | return LaravelNepaliDate::from($date) 17 | ->toNepaliDate( 18 | $format ?? config('nepali-date.default_format'), 19 | $locale ?? config('nepali-date.default_locale') 20 | ); 21 | } 22 | } 23 | 24 | if (! function_exists('toEnglishDate')) { 25 | /** 26 | * The function converts a given date to the English date format 27 | * 28 | * @param string $dateThe date parameter is a string that represents the date in the Nepali calendar format.
29 | * @param string|null $formatThe format parameter is used to specify the desired format of the English date.
30 | * @param string|null $localeThe "locale" parameter is used to specify the language and region. Supported languages are en and np
31 | * @return stringEnglish date converted from the given Nepali Date.
32 | */ 33 | function toEnglishDate(string $date, ?string $format = null, ?string $locale = null): string 34 | { 35 | return LaravelNepaliDate::from($date) 36 | ->toEnglishDate( 37 | $format ?? config('nepali-date.default_format'), 38 | $locale ?? config('nepali-date.default_locale') 39 | ); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "anuzpandey/laravel-nepali-date", 3 | "description": "A Laravel Package to convert English Date (A.D.) to Nepali Date (B.S.) and vice-versa.", 4 | "keywords": [ 5 | "anuzpandey", 6 | "laravel", 7 | "laravel-nepali-date", 8 | "nepali-date", 9 | "nepali-date-converter" 10 | ], 11 | "homepage": "https://laravel-nepali-date.anuzpandey.com", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "AnuzPandey", 16 | "email": "anuzbvbmaniac123@gmail.com", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^8.1", 22 | "spatie/laravel-package-tools": "^1.14.0", 23 | "illuminate/contracts": "^10.0|^11.0|^12.0" 24 | }, 25 | "require-dev": { 26 | "laravel/pint": "^1.0", 27 | "nunomaduro/collision": "^7.8", 28 | "orchestra/testbench": "^8.8", 29 | "pestphp/pest": "^2.20", 30 | "pestphp/pest-plugin-arch": "^2.0", 31 | "pestphp/pest-plugin-laravel": "^2.0" 32 | }, 33 | "autoload": { 34 | "psr-4": { 35 | "Anuzpandey\\LaravelNepaliDate\\": "src/" 36 | }, 37 | "files": [ 38 | "src/Helper/helper.php" 39 | ] 40 | }, 41 | "autoload-dev": { 42 | "psr-4": { 43 | "Anuzpandey\\LaravelNepaliDate\\Tests\\": "tests/", 44 | "Workbench\\App\\": "workbench/app/" 45 | } 46 | }, 47 | "scripts": { 48 | "post-autoload-dump": "@composer run prepare", 49 | "clear": "@php vendor/bin/testbench package:purge-laravel-nepali-date --ansi", 50 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 51 | "build": [ 52 | "@composer run prepare", 53 | "@php vendor/bin/testbench workbench:build --ansi" 54 | ], 55 | "start": [ 56 | "Composer\\Config::disableProcessTimeout", 57 | "@composer run build", 58 | "@php vendor/bin/testbench serve" 59 | ], 60 | "analyse": "vendor/bin/phpstan analyse", 61 | "test": "vendor/bin/pest", 62 | "test-coverage": "vendor/bin/pest --coverage", 63 | "format": "vendor/bin/pint" 64 | }, 65 | "config": { 66 | "sort-packages": true, 67 | "allow-plugins": { 68 | "pestphp/pest-plugin": true, 69 | "phpstan/extension-installer": true 70 | } 71 | }, 72 | "extra": { 73 | "laravel": { 74 | "providers": [ 75 | "Anuzpandey\\LaravelNepaliDate\\LaravelNepaliDateServiceProvider" 76 | ] 77 | } 78 | }, 79 | "minimum-stability": "dev", 80 | "prefer-stable": false 81 | } 82 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-12 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-extended-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Proper Method Signature and Its Default Values** - When modifying or adding methods, ensure that the method signatures are clear and concise. Provide default values for parameters when necessary, balancing flexibility with readability for a more maintainable codebase. 48 | 49 | - **Method Name Consistency** - Maintain consistency in naming conventions for methods throughout the package. Consistent naming practices improve code cohesion and readability, facilitating a smoother collaborative development process. 50 | 51 | - **Unnecessary Docblock** - Evaluate the necessity of docblock comments for methods. If a method's functionality is clear without additional explanation, consider omitting the docblock. This helps enhance code readability while preserving clarity. 52 | 53 | - **README Updates** - Ensure the `README.md` is updated along with changes to the codebase. Reflect any modifications to method names or signatures, providing users with accurate examples and explanations for a cohesive experience. 54 | 55 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 56 | 57 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 58 | 59 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 60 | 61 | **Happy coding**! 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Laravel Package to convert Dates from BS and AD. 2 | 3 | [](https://packagist.org/packages/anuzpandey/laravel-nepali-date) 4 | [](https://github.com/anuzpandey/laravel-nepali-date/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [](https://packagist.org/packages/anuzpandey/laravel-nepali-date) 6 | 7 | LaravelNepaliDate is a Laravel package that simplifies the conversion of dates between the Gregorian (English) and Nepali (Bikram Sambat) calendars. 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. 8 | 9 | ## Installation 10 | 11 | You can install the package via composer: 12 | 13 | ```bash 14 | composer require anuzpandey/laravel-nepali-date 15 | ``` 16 | 17 | Optionally, you can publish the config file with: 18 | 19 | ```bash 20 | php artisan vendor:publish --tag="nepali-date-config" 21 | ``` 22 | 23 | ## Usage 24 | 25 | ```php 26 | $engDate = '1996-04-22'; 27 | LaravelNepaliDate::from($engDate)->toNepaliDate(); 28 | // Result: 2053-01-10 29 | 30 | LaravelNepaliDate::from($engDate)->toNepaliDate(format: 'D, j F Y'); 31 | // Result: सोम, १० वैशाख २०५३ 32 | 33 | // Format Specifiers are supported and listed below 34 | LaravelNepaliDate::from($engDate)->toNepaliDate(format: 'D, j F Y', locale: 'en'); 35 | // Result: Mon, 10 Baisakh 2053 36 | 37 | 38 | $nepDate = '2053-01-10'; 39 | LaravelNepaliDate::from($nepDate)->toEnglishDate(); 40 | // Result: 1996-04-22 41 | 42 | LaravelNepaliDate::from($nepDate)->toEnglishDate(format: 'l, jS F Y'); 43 | // Result: Sunday, 22nd April 1996 44 | 45 | // Format Specifiers are supported and listed below 46 | LaravelNepaliDate::from($nepDate)->toEnglishDate(format: 'l, j F Y', locale: 'np'); 47 | // Result: आइतबार, २२ बैशाख १९९६ 48 | 49 | // Get total days in a month of a year 50 | use Anuzpandey\LaravelNepaliDate\Enums\NepaliMonth; 51 | // month can be NepaliMonth::XXX or month number (1-12) 52 | LaravelNepaliDate::daysInMonth(NepaliMonth::BAISAKH, 2053); 53 | // Result: 31 54 | 55 | // Get total days in a year 56 | LaravelNepaliDate::daysInYear(2053); 57 | // Result: 365 58 | ``` 59 | 60 | ## Format Specifiers 61 | 62 | The following format specifiers are supported for formatting dates: 63 | 64 | - `Y` - Year in four digits 65 | - `y` - Year in two digits 66 | - `m` - Month in two digits with leading zero (01-12/०१-१२) 67 | - `n` - Month in one or two digits without leading zero (1-12/१-१२) 68 | - `M` - Month in three letters (Jan-Dec) 69 | - `F` - Month in full name (January-December/बैशाख-चैत्र) 70 | - `d` - Day in two digits with leading zero (01-31/०१-३२) 71 | - `j` - Day in one or two digits without leading zero (1-31/१-३२) 72 | - `D` - Day in three letters (Sun-Sat/आइत-शनि) 73 | - `l` - Day in full name (Sunday-Saturday/आइतबार-शनिबार) 74 | - `S` - Day in two letters (st, nd, rd, th) 75 | 76 | ## Extending Carbon with NepaliDateMixin 77 | > **Note:** This feature has been deprecated as Carbon doesn't support the months having more than 31 days. This feature has been removed from version 2.0.0. 78 | 79 | ### Helper function 80 | 81 | ```php 82 | // Convert English date to Nepali date (B.S.). 83 | toNepaliDate("1996-04-22") 84 | // Result: 2053-01-10 85 | 86 | // Convert Nepali date to English date (A.D.). 87 | toEnglishDate("2053-01-10") 88 | // Result: 1996-04-22 89 | ``` 90 | 91 | ## Testing 92 | 93 | ```bash 94 | composer test 95 | ``` 96 | 97 | ## Changelog 98 | 99 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 100 | 101 | ## Contributing 102 | 103 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 104 | 105 | ## Security Vulnerabilities 106 | 107 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 108 | 109 | ## Credits 110 | 111 | - [AnuzPandey](https://github.com/anuzpandey) 112 | - [All Contributors](../../contributors) 113 | 114 | ## License 115 | 116 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 117 | -------------------------------------------------------------------------------- /src/Traits/HelperTrait.php: -------------------------------------------------------------------------------- 1 | getOrdinalSuffix((int) $formatData['j']); 100 | } else { 101 | $formattedString .= $formatData[$char]; 102 | } 103 | } 104 | } else { 105 | $formattedString .= $char; 106 | } 107 | } 108 | 109 | return $formattedString; 110 | } 111 | 112 | public function getOrdinalSuffix(int $number): string 113 | { 114 | if ($number % 100 >= 11 && $number % 100 <= 13) { 115 | return 'th'; 116 | } 117 | 118 | return match ($number % 10) { 119 | 1 => 'st', 120 | 2 => 'nd', 121 | 3 => 'rd', 122 | default => 'th', 123 | }; 124 | } 125 | 126 | private function getNepaliLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array 127 | { 128 | return [ 129 | 'Y' => $nepaliDateArray->npYear, 130 | 'y' => Str::substr($nepaliDateArray->npYear, 2, 2), 131 | 'F' => $nepaliDateArray->npMonthName, 132 | 'm' => $nepaliDateArray->npMonth, 133 | 'n' => $nepaliDateArray->npMonth > 9 ? $nepaliDateArray->npMonth : Str::substr($nepaliDateArray->npMonth, 1, 1), 134 | 'd' => $nepaliDateArray->npDay, 135 | 'j' => $nepaliDateArray->npDay > 9 ? $nepaliDateArray->npDay : Str::substr($nepaliDateArray->npDay, 1, 1), 136 | 'l' => $nepaliDateArray->npDayName, 137 | 'D' => $this->getShortDayName($nepaliDateArray->npDayName), 138 | ]; 139 | } 140 | 141 | private function getEnglishLocaleFormattingCharacters(NepaliDateArrayData $nepaliDateArray): array 142 | { 143 | return [ 144 | 'Y' => $nepaliDateArray->year, 145 | 'y' => Str::substr($nepaliDateArray->year, 2, 2), 146 | 'F' => $nepaliDateArray->monthName, 147 | 'm' => $nepaliDateArray->month, 148 | 'n' => $nepaliDateArray->month > 9 ? $nepaliDateArray->month : Str::substr($nepaliDateArray->month, 1, 1), 149 | 'd' => $nepaliDateArray->day, 150 | 'j' => $nepaliDateArray->day > 9 ? $nepaliDateArray->day : Str::substr($nepaliDateArray->day, 1, 1), 151 | 'l' => $nepaliDateArray->dayName, 152 | 'D' => $this->getShortDayName($nepaliDateArray->dayName, 'en'), 153 | ]; 154 | } 155 | 156 | private function formatDateString(string $format, string $locale, $dateArray): string 157 | { 158 | $formattedArray = ($locale === 'en') 159 | ? $this->getEnglishLocaleFormattingCharacters($dateArray) 160 | : $this->getNepaliLocaleFormattingCharacters($dateArray); 161 | 162 | $formatData = [ 163 | 'Y' => $formattedArray['Y'], 164 | 'y' => $formattedArray['y'], 165 | 'F' => $formattedArray['F'], 166 | 'm' => $formattedArray['m'], 167 | 'n' => $formattedArray['n'], 168 | 'd' => $formattedArray['d'], 169 | 'j' => $formattedArray['j'], 170 | 'l' => $formattedArray['l'], 171 | 'D' => $formattedArray['D'], 172 | 'S' => $formattedArray['j'], 173 | ]; 174 | 175 | return $this->getFormattedString($format, $formatData, $locale); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/Traits/EnglishDateTrait.php: -------------------------------------------------------------------------------- 1 | 'January', 12 | 2 => 'February', 13 | 3 => 'March', 14 | 4 => 'April', 15 | 5 => 'May', 16 | 6 => 'June', 17 | 7 => 'July', 18 | 8 => 'August', 19 | 9 => 'September', 20 | 10 => 'October', 21 | 11 => 'November', 22 | 12 => 'December', 23 | ]; 24 | 25 | private array $englishMonthInNepali = [ 26 | 1 => 'जनवरी', 27 | 2 => 'फेब्रुअरी', 28 | 3 => 'मार्च', 29 | 4 => 'अप्रिल', 30 | 5 => 'मे', 31 | 6 => 'जुन', 32 | 7 => 'जुलाई', 33 | 8 => 'अगस्ट', 34 | 9 => 'सेप्टेम्बर', 35 | 10 => 'अक्टोबर', 36 | 11 => 'नोभेम्बर', 37 | 12 => 'डिसेम्बर', 38 | ]; 39 | 40 | private array $dayOfWeekInEnglish = [ 41 | 1 => 'Sunday', 42 | 2 => 'Monday', 43 | 3 => 'Tuesday', 44 | 4 => 'Wednesday', 45 | 5 => 'Thursday', 46 | 6 => 'Friday', 47 | 7 => 'Saturday', 48 | ]; 49 | 50 | private array $numbersInEnglish = [ 51 | 0 => '0', 52 | 1 => '1', 53 | 2 => '2', 54 | 3 => '3', 55 | 4 => '4', 56 | 5 => '5', 57 | 6 => '6', 58 | 7 => '7', 59 | 8 => '8', 60 | 9 => '9', 61 | ]; 62 | 63 | public function toEnglishDate(?string $format = null, ?string $locale = null): string 64 | { 65 | $checkIfIsInRange = $this->isInNepaliDateRange($this->year, $this->month, $this->day); 66 | 67 | if (! $checkIfIsInRange) { 68 | throw new RuntimeException($checkIfIsInRange); 69 | } 70 | 71 | $totalNepaliDays = $this->calculateTotalNepaliDays(); 72 | 73 | $this->performCalculationBasedonNepaliDays($totalNepaliDays); 74 | 75 | return $this->toFormattedEnglishDate( 76 | $format ?? config('nepali-date.default_format'), 77 | $locale ?? config('nepali-date.default_locale'), 78 | ); 79 | } 80 | 81 | public function toEnglishDateArray(): NepaliDateArrayData 82 | { 83 | return NepaliDateArrayData::from([ 84 | 'year' => $this->englishYear, 85 | 'month' => $this->englishMonth, 86 | 'day' => $this->englishDay, 87 | 'npYear' => $this->convertEnToNpNumber($this->englishYear), 88 | 'npMonth' => $this->convertEnToNpNumber($this->englishMonth), 89 | 'npDay' => $this->convertEnToNpNumber($this->englishDay), 90 | 'dayName' => $this->dayOfWeekInEnglish[$this->dayOfWeek], 91 | 'monthName' => $this->monthsInEnglish[(int) $this->englishMonth], 92 | 'npDayName' => $this->formattedNepaliDateOfWeek($this->dayOfWeek), 93 | 'npMonthName' => $this->englishMonthInNepali[(int) $this->englishMonth], 94 | ]); 95 | } 96 | 97 | public function isInNepaliDateRange(int $year, int $month, int $day): string|bool 98 | { 99 | if ($year < 2000 || $year > 2089) { 100 | return 'Date is out of range. Please provide date between 2000 to 2089'; 101 | } 102 | 103 | if ($month < 1 || $month > 12) { 104 | return 'Month is out of range. Please provide month between 1-12'; 105 | } 106 | 107 | if ($day < 1 || $day > 32) { 108 | return 'Day is out of range. Please provide day between 1-32'; 109 | } 110 | 111 | return true; 112 | } 113 | 114 | public function calculateTotalNepaliDays() 115 | { 116 | $totalNepaliDays = 0; 117 | $k = 0; 118 | 119 | for ($i = 0; $i < ($this->year - $this->nepaliYear); $i++) { 120 | for ($j = 1; $j <= 12; $j++) { 121 | $totalNepaliDays += $this->calendarData[$k][$j]; 122 | } 123 | $k++; 124 | } 125 | 126 | // Count Total Days in terms of month 127 | for ($j = 1; $j < $this->month; $j++) { 128 | $totalNepaliDays += $this->calendarData[$k][$j]; 129 | } 130 | 131 | // Count Total Days in Terms of days 132 | $totalNepaliDays += $this->day; 133 | 134 | return $totalNepaliDays; 135 | } 136 | 137 | public function performCalculationBasedOnNepaliDays(string|int $totalNepaliDays): void 138 | { 139 | $_day = 4 - 1; 140 | 141 | // Calculation of equivalent english date... 142 | $_year = $this->englishYear; 143 | $_month = $this->englishMonth; 144 | $totalEnglishDays = $this->englishDay; 145 | 146 | while ($totalNepaliDays != 0) { 147 | $a = ($this->isLeapYear($_year)) 148 | ? $this->englishLeapMonths[$_month] 149 | : $this->englishNormalMonths[$_month]; 150 | 151 | $totalEnglishDays++; 152 | $_day++; 153 | 154 | if ($totalEnglishDays > $a) { 155 | $_month++; 156 | $totalEnglishDays = 1; 157 | if ($_month > 12) { 158 | $_year++; 159 | $_month = 1; 160 | } 161 | } 162 | 163 | if ($_day > 7) { 164 | $_day = 1; 165 | } 166 | 167 | $totalNepaliDays--; 168 | } 169 | 170 | $this->englishYear = $_year; 171 | $this->englishMonth = $_month > 9 ? $_month : '0'.$_month; 172 | $this->englishDay = $totalEnglishDays > 9 ? $totalEnglishDays : '0'.$totalEnglishDays; 173 | $this->dayOfWeek = $_day; 174 | } 175 | 176 | public function toFormattedEnglishDate(string $format, string $locale): string 177 | { 178 | $englishDateArray = $this->toEnglishDateArray(); 179 | 180 | return $this->formatDateString($format, $locale, $englishDateArray); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /src/Constants/NepaliDate.php: -------------------------------------------------------------------------------- 1 | [2000, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 9 | 1 => [2001, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 10 | 2 => [2002, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 11 | 3 => [2003, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 12 | 4 => [2004, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 13 | 5 => [2005, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 14 | 6 => [2006, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 15 | 7 => [2007, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 16 | 8 => [2008, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 17 | 9 => [2009, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 18 | 10 => [2010, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 19 | 11 => [2011, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 20 | 12 => [2012, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 21 | 13 => [2013, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 22 | 14 => [2014, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 23 | 15 => [2015, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 24 | 16 => [2016, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 25 | 17 => [2017, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 26 | 18 => [2018, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 27 | 19 => [2019, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 28 | 20 => [2020, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 29 | 21 => [2021, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 30 | 22 => [2022, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 31 | 23 => [2023, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 32 | 24 => [2024, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 33 | 25 => [2025, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 34 | 26 => [2026, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 35 | 27 => [2027, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 36 | 28 => [2028, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 37 | 29 => [2029, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], 38 | 30 => [2030, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 39 | 31 => [2031, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 40 | 32 => [2032, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 41 | 33 => [2033, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 42 | 34 => [2034, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 43 | 35 => [2035, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 44 | 36 => [2036, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 45 | 37 => [2037, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 46 | 38 => [2038, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 47 | 39 => [2039, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 48 | 40 => [2040, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 49 | 41 => [2041, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 50 | 42 => [2042, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 51 | 43 => [2043, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 52 | 44 => [2044, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 53 | 45 => [2045, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 54 | 46 => [2046, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 55 | 47 => [2047, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 56 | 48 => [2048, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 57 | 49 => [2049, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 58 | 50 => [2050, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 59 | 51 => [2051, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 60 | 52 => [2052, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 61 | 53 => [2053, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 62 | 54 => [2054, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 63 | 55 => [2055, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 64 | 56 => [2056, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], 65 | 57 => [2057, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 66 | 58 => [2058, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 67 | 59 => [2059, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 68 | 60 => [2060, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 69 | 61 => [2061, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 70 | 62 => [2062, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], 71 | 63 => [2063, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 72 | 64 => [2064, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 73 | 65 => [2065, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 74 | 66 => [2066, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 75 | 67 => [2067, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 76 | 68 => [2068, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 77 | 69 => [2069, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 78 | 70 => [2070, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 79 | 71 => [2071, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 80 | 72 => [2072, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 81 | 73 => [2073, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 82 | 74 => [2074, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 83 | 75 => [2075, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 84 | 76 => [2076, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 85 | 77 => [2077, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 86 | 78 => [2078, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 87 | 79 => [2079, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 88 | 80 => [2080, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 89 | 81 => [2081, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 90 | 82 => [2082, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 91 | 83 => [2083, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 92 | 84 => [2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 93 | 85 => [2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], 94 | 86 => [2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 95 | 87 => [2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], 96 | 88 => [2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], 97 | 89 => [2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 98 | 90 => [2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 99 | 91 => [2091, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], 100 | 92 => [2092, 30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], 101 | 93 => [2093, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 102 | 94 => [2094, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 103 | 95 => [2095, 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30], 104 | 96 => [2096, 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 105 | 97 => [2097, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 106 | 98 => [2098, 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31], 107 | 99 => [2099, 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30], 108 | ]; 109 | } 110 | -------------------------------------------------------------------------------- /src/Traits/CalendarDateDataTrait.php: -------------------------------------------------------------------------------- 1 | [2000, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 9 | 1 => [2001, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 10 | 2 => [2002, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 11 | 3 => [2003, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 12 | 4 => [2004, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 13 | 5 => [2005, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 14 | 6 => [2006, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 15 | 7 => [2007, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 16 | 8 => [2008, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 17 | 9 => [2009, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 18 | 10 => [2010, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 19 | 11 => [2011, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 20 | 12 => [2012, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 21 | 13 => [2013, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 22 | 14 => [2014, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 23 | 15 => [2015, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 24 | 16 => [2016, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 25 | 17 => [2017, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 26 | 18 => [2018, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 27 | 19 => [2019, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 28 | 20 => [2020, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 29 | 21 => [2021, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 30 | 22 => [2022, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 31 | 23 => [2023, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 32 | 24 => [2024, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 33 | 25 => [2025, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 34 | 26 => [2026, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 35 | 27 => [2027, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 36 | 28 => [2028, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 37 | 29 => [2029, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], 38 | 30 => [2030, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 39 | 31 => [2031, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 40 | 32 => [2032, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 41 | 33 => [2033, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 42 | 34 => [2034, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 43 | 35 => [2035, 30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 44 | 36 => [2036, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 45 | 37 => [2037, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 46 | 38 => [2038, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 47 | 39 => [2039, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 48 | 40 => [2040, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 49 | 41 => [2041, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 50 | 42 => [2042, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 51 | 43 => [2043, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 52 | 44 => [2044, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 53 | 45 => [2045, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 54 | 46 => [2046, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 55 | 47 => [2047, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 56 | 48 => [2048, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 57 | 49 => [2049, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 58 | 50 => [2050, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 59 | 51 => [2051, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 60 | 52 => [2052, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 61 | 53 => [2053, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 62 | 54 => [2054, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 63 | 55 => [2055, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 64 | 56 => [2056, 31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], 65 | 57 => [2057, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 66 | 58 => [2058, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 67 | 59 => [2059, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 68 | 60 => [2060, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 69 | 61 => [2061, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 70 | 62 => [2062, 30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], 71 | 63 => [2063, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 72 | 64 => [2064, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 73 | 65 => [2065, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 74 | 66 => [2066, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], 75 | 67 => [2067, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 76 | 68 => [2068, 31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 77 | 69 => [2069, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 78 | 70 => [2070, 31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], 79 | 71 => [2071, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 80 | 72 => [2072, 31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], 81 | 73 => [2073, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], 82 | 74 => [2074, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 83 | 75 => [2075, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 84 | 76 => [2076, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 85 | 77 => [2077, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 86 | 78 => [2078, 31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], 87 | 79 => [2079, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 88 | 80 => [2080, 31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], 89 | 81 => [2081, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], 90 | 82 => [2082, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 91 | 83 => [2083, 31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], 92 | 84 => [2084, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 93 | 85 => [2085, 31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], 94 | 86 => [2086, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 95 | 87 => [2087, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], 96 | 88 => [2088, 30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], 97 | 89 => [2089, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 98 | 90 => [2090, 30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 99 | 91 => [2091, 31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], 100 | 92 => [2092, 30, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], 101 | 93 => [2093, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 102 | 94 => [2094, 31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], 103 | 95 => [2095, 31, 31, 32, 31, 31, 31, 30, 29, 30, 30, 30, 30], 104 | 96 => [2096, 30, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], 105 | 97 => [2097, 31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], 106 | 98 => [2098, 31, 31, 32, 31, 31, 31, 29, 30, 29, 30, 29, 31], 107 | 99 => [2099, 31, 31, 32, 31, 31, 31, 30, 29, 29, 30, 30, 30], 108 | ]; 109 | 110 | private array $normalMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 111 | 112 | private array $leapMonths = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 113 | 114 | private array $englishNormalMonths = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 115 | 116 | private array $englishLeapMonths = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 117 | 118 | /* These are initial values for nepali date. */ 119 | private string|int $nepaliYear = 2000; 120 | 121 | private string|int $nepaliMonth = 9; 122 | 123 | private string|int $nepaliDay = 16; // 17 - 1 124 | 125 | private string|int $dayOfWeek = 6; // 7 - 1, 0 for sunday, 6 for saturday 126 | 127 | private string|int $englishYear = 1943; 128 | 129 | private string|int $englishMonth = 4; 130 | 131 | private string|int $englishDay = 13; // 14 - 1 132 | } 133 | -------------------------------------------------------------------------------- /src/Traits/NepaliDateTrait.php: -------------------------------------------------------------------------------- 1 | 'वैशाख', 14 | 2 => 'जेठ', 15 | 3 => 'असार', 16 | 4 => 'साउन', 17 | 5 => 'भदौ', 18 | 6 => 'असोज', 19 | 7 => 'कात्तिक', 20 | 8 => 'मंसिर', 21 | 9 => 'पुस', 22 | 10 => 'माघ', 23 | 11 => 'फागुन', 24 | 12 => 'चैत', 25 | ]; 26 | 27 | private array $nepaliMonthInEnglish = [ 28 | 1 => 'Baisakh', 29 | 2 => 'Jestha', 30 | 3 => 'Asar', 31 | 4 => 'Shrawan', 32 | 5 => 'Bhadra', 33 | 6 => 'Aswin', 34 | 7 => 'Kartik', 35 | 8 => 'Mangsir', 36 | 9 => 'Poush', 37 | 10 => 'Magh', 38 | 11 => 'Falgun', 39 | 12 => 'Chaitra', 40 | ]; 41 | 42 | private array $dayOfWeekInNepali = [ 43 | 1 => 'आइतबार', 44 | 2 => 'सोमबार', 45 | 3 => 'मङ्गलबार', 46 | 4 => 'बुधबार', 47 | 5 => 'बिहिबार', 48 | 6 => 'शुक्रबार', 49 | 7 => 'शनिबार', 50 | ]; 51 | 52 | private array $numbersInNepali = [ 53 | 0 => '०', 54 | 1 => '१', 55 | 2 => '२', 56 | 3 => '३', 57 | 4 => '४', 58 | 5 => '५', 59 | 6 => '६', 60 | 7 => '७', 61 | 8 => '८', 62 | 9 => '९', 63 | ]; 64 | 65 | public static function daysInMonth(string|int|NepaliMonth $month, string|int $year): int 66 | { 67 | $monthIndex = is_string($month) 68 | ? NepaliMonth::from($month)->value 69 | : ($month instanceof NepaliMonth ? $month->value : (int) $month); 70 | 71 | $yearIndex = self::getCalendarYearIndex($year); 72 | 73 | if ($yearIndex === false) { 74 | throw new RuntimeException('Year is out of range. Please provide a year between 2000-2090'); 75 | } 76 | 77 | return NepaliDate::$CALENDAR_DATA[$yearIndex][$monthIndex]; 78 | } 79 | 80 | public static function daysInYear(string|int $year): int 81 | { 82 | $yearIndex = self::getCalendarYearIndex($year); 83 | 84 | if ($yearIndex === false) { 85 | throw new RuntimeException('Year is out of range. Please provide a year between 2000-2090'); 86 | } 87 | 88 | return array_sum(array_slice(NepaliDate::$CALENDAR_DATA[$yearIndex], 1)); 89 | } 90 | 91 | public static function getCalendarYearIndex($year): bool|int 92 | { 93 | $calendarData = NepaliDate::$CALENDAR_DATA; 94 | 95 | return collect($calendarData)->search(fn ($data) => $data[0] === (int) $year); 96 | } 97 | 98 | public function toNepaliDate(?string $format = null, ?string $locale = null): string 99 | { 100 | $this->performCalculationOnEnglishDate(); 101 | 102 | return $this->toFormattedNepaliDate($format, $locale); 103 | } 104 | 105 | public function toFormattedNepaliDate(?string $format = null, ?string $locale = null): string 106 | { 107 | $format = $format ?? config('nepali-date.default_format'); 108 | $locale = $locale ?? config('nepali-date.default_locale'); 109 | 110 | $nepaliDateArray = $this->toNepaliDateArrayData(); 111 | 112 | return $this->formatDateString($format, $locale, $nepaliDateArray); 113 | } 114 | 115 | public function toNepaliDateArray(): NepaliDateArrayData 116 | { 117 | $this->performCalculationOnEnglishDate(); 118 | 119 | return $this->toNepaliDateArrayData(); 120 | } 121 | 122 | private function toNepaliDateArrayData(): NepaliDateArrayData 123 | { 124 | $nepaliMonth = $this->nepaliMonth > 9 ? $this->nepaliMonth : '0'.$this->nepaliMonth; 125 | $nepaliDay = $this->nepaliDay > 9 ? $this->nepaliDay : '0'.$this->nepaliDay; 126 | 127 | return NepaliDateArrayData::from([ 128 | 'year' => $this->nepaliYear, 129 | 'month' => $nepaliMonth, 130 | 'day' => $nepaliDay, 131 | 'npYear' => $this->formattedNepaliNumber($this->nepaliYear), 132 | 'npMonth' => $this->formattedNepaliNumber($nepaliMonth), 133 | 'npDay' => $this->formattedNepaliNumber($nepaliDay), 134 | 'dayName' => $this->dayOfWeekInEnglish[$this->dayOfWeek], 135 | 'monthName' => $this->nepaliMonthInEnglish[$this->nepaliMonth], 136 | 'npDayName' => $this->formattedNepaliDateOfWeek($this->dayOfWeek), 137 | 'npMonthName' => $this->monthsInNepali[$this->nepaliMonth], 138 | ]); 139 | } 140 | 141 | public function getShortDayName(string $npDayName, string $locale = 'np'): string 142 | { 143 | if ($locale === 'en') { 144 | return match ($npDayName) { 145 | 'Sunday' => 'Sun', 146 | 'Monday' => 'Mon', 147 | 'Tuesday' => 'Tue', 148 | 'Wednesday' => 'Wed', 149 | 'Thursday' => 'Thu', 150 | 'Friday' => 'Fri', 151 | 'Saturday' => 'Sat', 152 | }; 153 | } 154 | 155 | return match ($npDayName) { 156 | 'आइतबार' => 'आइत', 157 | 'सोमबार' => 'सोम', 158 | 'मङ्गलबार' => 'मङ्गल', 159 | 'बुधबार' => 'बुध', 160 | 'बिहिबार' => 'बिहि', 161 | 'शुक्रबार' => 'शुक्र', 162 | 'शनिबार' => 'शनि', 163 | }; 164 | } 165 | 166 | public function performCalculationOnEnglishDate(): void 167 | { 168 | $checkIfIsInRange = $this->isInEnglishDateRange($this->year, $this->month, $this->day); 169 | 170 | if (! $checkIfIsInRange) { 171 | throw new RuntimeException($checkIfIsInRange); 172 | } 173 | 174 | $totalEnglishDays = $this->calculateTotalEnglishDays($this->year, $this->month, $this->day); 175 | 176 | $this->performCalculationBasedOn($totalEnglishDays); 177 | } 178 | 179 | private function calculateTotalEnglishDays($year, $month, $day) 180 | { 181 | $totalEnglishDays = 0; 182 | 183 | for ($i = 0; $i < ($year - 1944); $i++) { 184 | if ($this->isLeapYear(1944 + $i)) { 185 | for ($j = 0; $j < 12; $j++) { 186 | $totalEnglishDays += $this->leapMonths[$j]; 187 | } 188 | } else { 189 | for ($j = 0; $j < 12; $j++) { 190 | $totalEnglishDays += $this->normalMonths[$j]; 191 | } 192 | } 193 | } 194 | 195 | for ($i = 0; $i < ($month - 1); $i++) { 196 | if ($this->isLeapYear($year)) { 197 | $totalEnglishDays += $this->leapMonths[$i]; 198 | } else { 199 | $totalEnglishDays += $this->normalMonths[$i]; 200 | } 201 | } 202 | 203 | $totalEnglishDays += $day; 204 | 205 | return $totalEnglishDays; 206 | } 207 | 208 | private function performCalculationBasedOn($totalEnglishDays): void 209 | { 210 | $i = 0; 211 | $j = $this->nepaliMonth; 212 | 213 | while ($totalEnglishDays != 0) { 214 | $lastDayOfMonth = $this->calendarData[$i][$j]; 215 | 216 | $this->nepaliDay++; 217 | $this->dayOfWeek++; 218 | 219 | if ($this->nepaliDay > $lastDayOfMonth) { 220 | $this->nepaliMonth++; 221 | $this->nepaliDay = 1; 222 | $j++; 223 | } 224 | 225 | if ($this->dayOfWeek > 7) { 226 | $this->dayOfWeek = 1; 227 | } 228 | 229 | if ($this->nepaliMonth > 12) { 230 | $this->nepaliYear++; 231 | $this->nepaliMonth = 1; 232 | } 233 | 234 | if ($j > 12) { 235 | $j = 1; 236 | $i++; 237 | } 238 | 239 | $totalEnglishDays--; 240 | } 241 | } 242 | 243 | private function formattedNepaliDateOfWeek($dayOfWeek) 244 | { 245 | return $this->dayOfWeekInNepali[$dayOfWeek]; 246 | } 247 | 248 | private function formattedNepaliNumber($value): string 249 | { 250 | $numbers = str_split($value); 251 | 252 | foreach ($numbers as $key => $number) { 253 | $numbers[$key] = $this->numbersInNepali[$number]; 254 | } 255 | 256 | return implode('', $numbers); 257 | } 258 | 259 | private function isInEnglishDateRange(int $year, int $month, int $day): string|bool 260 | { 261 | if ($year < 1944 || $year > 2033) { 262 | return 'Date is out of range. Please provide date between 1944-01-01 to 2033-12-31'; 263 | } 264 | 265 | if ($month < 1 || $month > 12) { 266 | return 'Month is out of range. Please provide month between 1-12'; 267 | } 268 | 269 | if ($day < 1 || $day > 31) { 270 | return 'Day is out of range. Please provide day between 1-31'; 271 | } 272 | 273 | return true; 274 | } 275 | } 276 | --------------------------------------------------------------------------------