composer require mr.incognito/date-converter in your Laravel project.use MrIncognito\DateConverter\Facades\DateConverter;
$bsDate = DateConverter::currentBsDate('Y-m-d'); // e.g., "2081-11-28"
config/date-converter.php (if published): Check for customizable settings like default formats.app/Providers/AppServiceProvider.php: Verify facade binding (usually auto-registered).Date Conversion:
fromAdToBs($adDate, $format) for explicit conversions.
$bsDate = DateConverter::fromAdToBs('2025-03-12', 'Y-m-d'); // "2081-11-28"
fromBsToAd($bsDate, $format).
$adDate = DateConverter::fromBsToAd('2081-11-28', 'Y-m-d'); // "2025-03-12"
currentBsDate()/currentAdDate() for dynamic values.Detailed Date Objects:
$bsDetails = DateConverter::currentBsDateDetail();
// Returns array with year, month, day, week_day (Nepali), month_name, etc.
Integration with Eloquent:
class Event extends Model {
public function getBsDateAttribute() {
return DateConverter::fromAdToBs($this->ad_date, 'Y-m-d');
}
}
public function scopeBsDateAfter($query, $date) {
$adDate = DateConverter::fromBsToAd($date);
return $query->where('ad_date', '>=', $adDate);
}
Form Handling:
$validator = Validator::make($request->all(), [
'bs_date' => 'required|date_format:Y-m-d',
]);
$adDate = $validator->passes() ? DateConverter::fromBsToAd($request->bs_date) : null;
Localization:
month_name/week_day from *DateDetail() for Nepali labels in views:
{{ DateConverter::currentBsDateDetail()['month_name'] }} {{ DateConverter::currentBsDateDetail()['year'] }}
Date Format Strictness:
Y-m-d). Non-compliant formats (e.g., m/d/Y) may fail silently or return incorrect results.DateTime::createFromFormat() before conversion.Time Zone Sensitivity:
date_default_timezone_set() if working across environments.DateConverter::setTimeZone('Asia/Kathmandu') if available (check package docs for extension points).Leap Year Quirks:
2080-02-30 BS) may throw errors.$bsDate = DateConverter::fromAdToBs('2023-02-28');
logger($bsDate); // Should be "2079-11-15" (not 11-16)
Facade vs. Helper:
DateConverter), but may also expose static helpers. Prefer the facade for consistency and testability.Database Storage:
Custom Formats:
php artisan vendor:publish --provider="MrIncognito\DateConverter\DateConverterServiceProvider"
config/date-converter.php:
'defaults' => [
'ad_format' => 'Y-m-d',
'bs_format' => 'Y-m-d',
],
Testing:
$this->mock(DateConverter::class)->shouldReceive('currentBsDate')->andReturn('2081-11-28');
Performance:
$cachedBsDate = Cache::remember('bs_today', now()->addDays(1), function() {
return DateConverter::currentBsDate();
});
Extending Functionality:
class CustomDateConverter extends \MrIncognito\DateConverter\DateConverter {
public static function fromAdToBsWithEra($adDate) {
$bsDate = parent::fromAdToBs($adDate);
return "BS {$bsDate} (विसं)";
}
}
Fallback for Invalid Dates:
try {
$bsDate = DateConverter::fromAdToBs('invalid-date');
} catch (\Exception $e) {
$bsDate = 'N/A';
}
Documentation Gaps:
function addBsDays($bsDate, $days) {
$adDate = DateConverter::fromBsToAd($bsDate);
$newAdDate = (new DateTime($adDate))->add(new DateInterval("P{$days}D"));
return DateConverter::fromAdToBs($newAdDate->format('Y-m-d'));
}
How can I help you explore Laravel packages today?