Skip to content

Commit

Permalink
Merge pull request #9 from dineshuprety/helperfunction
Browse files Browse the repository at this point in the history
Add Helper Function `toNepaliDate()` and `toEnglishDate()`.
  • Loading branch information
anuzpandey authored Nov 22, 2023
2 parents 31b3e92 + 836af51 commit 9d1dce4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,16 @@ Carbon::parse($engDate)->toNepaliDate(format: 'D, j F Y');
// Result: सोम, १० वैशाख २०५३
```

### Helper function
```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
```
## Testing

```bash
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"psr-4": {
"Anuzpandey\\LaravelNepaliDate\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/"
}
},
"files": [
"src/Helper/helper.php"
]
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
Expand Down
40 changes: 40 additions & 0 deletions src/Helper/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Anuzpandey\LaravelNepaliDate\LaravelNepaliDate;

if (!function_exists('toNepaliDate')) {
/**
* The function converts a given date to the Nepali date format
*
* @param string date The date parameter is a string that represents the date in the Gregorian
* calendar format. It should be in the format "YYYY-MM-DD" or "YYYY/MM/DD".
* @param string|null format The format parameter is used to specify the desired format of the Nepali
* date. It is an optional parameter and if not provided, the default format will be used.
* @param string|null locale The "locale" parameter is used to specify the language and region
* @return string Nepali date converted from the given date.
*/
function toNepaliDate($date, $format = 'Y-m-d', $locale = 'en'): string
{
return LaravelNepaliDate::from($date)
->toNepaliDate($format, $locale);
}
}


if (!function_exists('toEnglishDate')) {
/**
* The function converts a given date from Nepali (Bikram Sambat) to English format
*
* @param string date The date parameter is a string that represents the date in the Gregorian
* calendar format. It should be in the format "YYYY-MM-DD" or "YYYY/MM/DD".
* @param string|null format The format parameter is used to specify the desired format of the English
* date. It is an optional parameter and if not provided, the default format will be used.
* @param string|null locale The "locale" parameter is used to specify the language and region
* @return string English date converted from the given Nepali Date.
*/
function toEnglishDate($date, $format = 'Y-m-d', $locale = 'en'): string
{
return LaravelNepaliDate::from($date)
->toEnglishDate($format, $locale);
}
}
25 changes: 25 additions & 0 deletions tests/ConvertToEnglishDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,28 @@
['d F Y', 'en', '22 April 1996'],
['Y/m/d', 'np', '१९९६/०४/२२'],
]);

it('can convert to basic nepali date to english date with helper function', function (string $date, string $expectedResult) {
expect(toEnglishDate($date))
->toBe($expectedResult);
})->with([
['2053-01-10', '1996-04-22'],
['2029-04-04', '1972-07-19'],
['2022-12-20', '1966-04-02'],
]);


it('can convert to nepali formatted result to english date with helper function', function (string $format, string $locale, string $expectedResult) {
expect(toEnglishDate("2053-01-10", $format, $locale))
->toBe($expectedResult);
})->with([
['d F Y, l', 'np', '२२ अप्रिल १९९६, सोमबार'],
['d F Y, l', 'en', '22 April 1996, Monday'],
['Y-m-d', 'np', '१९९६-०४-२२'],
['Y-m-d', 'en', '1996-04-22'],
['l, d F Y', 'np', 'सोमबार, २२ अप्रिल १९९६'],
['l, d F Y', 'en', 'Monday, 22 April 1996'],
['d F Y', 'np', '२२ अप्रिल १९९६'],
['d F Y', 'en', '22 April 1996'],
['Y/m/d', 'np', '१९९६/०४/२२'],
]);
25 changes: 25 additions & 0 deletions tests/ConvertToNepaliDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@
['d F Y', 'en', '10 Baisakh 2053'],
['Y/m/d', 'np', '२०५३/०१/१०'],
]);


it('can convert to basic nepali date with helper function toNepaliDate', function (string $date, string $expectedResult) {
expect(toNepaliDate($date))
->toBe($expectedResult);
})->with([
['1996-04-22', '2053-01-10'],
['1972-07-19', '2029-04-04'],
['1966-04-02', '2022-12-20'],
]);

it('can convert to nepali formatted result with helper function toNepaliDate', function (string $format, string $locale, string $expectedResult) {
expect(toNepaliDate("1996-04-22", $format, $locale))
->toBe($expectedResult);
})->with([
['d F Y, l', 'np', '१० वैशाख २०५३, सोमबार'],
['d F Y, l', 'en', '10 Baisakh 2053, Monday'],
['Y-m-d', 'np', '२०५३-०१-१०'],
['Y-m-d', 'en', '2053-01-10'],
['l, d F Y', 'np', 'सोमबार, १० वैशाख २०५३'],
['l, d F Y', 'en', 'Monday, 10 Baisakh 2053'],
['d F Y', 'np', '१० वैशाख २०५३'],
['d F Y', 'en', '10 Baisakh 2053'],
['Y/m/d', 'np', '२०५३/०१/१०'],
]);

0 comments on commit 9d1dce4

Please sign in to comment.