Skip to content

Instantly share code, notes, and snippets.

@wovosoft
Last active December 26, 2023 17:09
Show Gist options
  • Save wovosoft/9b4bd4f9b340c72c8ba4de04ce8e8d24 to your computer and use it in GitHub Desktop.
Save wovosoft/9b4bd4f9b340c72c8ba4de04ce8e8d24 to your computer and use it in GitHub Desktop.
English to Bengali Calendar Date Converter for Laravel Framework
<?php
namespace App\Helpers;
use Illuminate\Support\Carbon;
use Illuminate\Support\Number;
/**
* @author Narayan Adhikar
* @link https://gist.github.com/wovosoft/9b4bd4f9b340c72c8ba4de04ce8e8d24
* Laravel version of
* @see https://github.com/tareq1988/bangla-date/blob/master/class.banglaDate.php
*/
class BengaliDate
{
private int $day;
private int $month;
private int $year;
public const StartsAtNight = 24;
public const StartsAtMorning = 6;
const Months = [
"পৌষ", "মাঘ", "ফাল্গুন", "চৈত্র", "বৈশাখ", "জ্যৈষ্ঠ", "আষাঢ়", "শ্রাবণ", "ভাদ্র", "আশ্বিন", "কার্তিক", "অগ্রহায়ণ"
];
const LastDayInMonths = [
30, 30, 30, 30, 31, 31, 31, 31, 31, 30, 30, 30
];
const MidDates = [
13, 12, 14, 13, 14, 14, 15, 15, 15, 15, 14, 14
];
public function __construct(
private readonly Carbon $timestamp,
private readonly int $morning = self::StartsAtNight
)
{
[$this->day, $this->month, $this->year] = $this->convert();
}
public static function createFrom(string|Carbon $date, int $morning = self::StartsAtNight): static
{
if ($date instanceof Carbon) {
return new static($date, $morning);
}
return new static(Carbon::parse($date), $morning);
}
/**
* @param string $format %M = Short Month, %Y = Full Year, %y = Short Year, %d = Day
* @return string
*/
public function format(string $format): string
{
return strtr($format, [
'%M' => self::Months[$this->month],
'%m' => str(Number::format($this->month + 1, locale: 'bn-BD'))
->replace(',', '')
->padLeft(2, '০')
->value(),
'%Y' => str(Number::format($this->year, locale: 'bn-BD'))
->replace(',', '')
->value(),
'%y' => str(Number::format(str($this->year)->substr(2, 2)->toInteger(), locale: 'bn-BD'))
->replace(',', '')
->value(),
'%d' => Number::format($this->day, locale: 'bn-BD')
]);
}
private function convert(): array
{
$days = $this->timestamp->day - self::MidDates[$this->timestamp->month - 1];
$hours = $this->timestamp->format('G');
if ($hours < $this->morning) {
$days -= 1;
}
if (
($this->timestamp->day <= self::MidDates[$this->timestamp->month - 1])
|| ($this->timestamp->day == self::MidDates[$this->timestamp->month - 1] + 1 && $hours < $this->morning)
) {
$days += self::LastDayInMonths[$this->timestamp->month - 1];
if ($this->timestamp->isLeapYear()) {
$days += 1;
}
$months = $this->timestamp->month - 1;
} else {
$months = ($this->timestamp->month) % 12;
}
$year = $this->timestamp->year - 593;
if (($this->timestamp->month < 4) || (($this->timestamp->month == 4) && (($this->timestamp->day < 14) || ($this->timestamp->day == 14 && $hours < $this->morning)))) {
$year -= 1;
}
return [
$days, $months, $year
];
}
}
@wovosoft
Copy link
Author

** Usage **

\App\Helpers\BengaliDate::createFrom(now())->format("%d %M %Y বঙ্গাব্দ");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment