Documentation
Installation
You can install the package via Composer:
composer require asorasoft/chhankitek
The package will automatically register its service provider. No additional configuration is needed.
Basic Usage
Using the Trait
In your Laravel controller, use the HasChhankitek trait:
use Asorasoft\Chhankitek\Traits\HasChhankitek;
use Carbon\CarbonImmutable;
class YourController extends Controller
{
use HasChhankitek;
public function index()
{
$lunarDate = $this->chhankiteck(
CarbonImmutable::now()->setTimezone('Asia/Phnom_Penh')
);
echo $lunarDate->toString();
// Output: ααααα
αααα α€ ααα
ααααααΆααΆα ααααΆαααααΌα ααααΈααα αα»αααααααΆα α’α₯α¦α₯
}
}
Using the Helper Function
Alternatively, you can use the global toLunarDate helper:
use Carbon\CarbonImmutable;
$lunarDate = toLunarDate(
CarbonImmutable::now()->setTimezone('Asia/Phnom_Penh')
);
echo $lunarDate->toString();
Important: Always use CarbonImmutable
(not Carbon) and set the timezone to 'Asia/Phnom_Penh'.
Available Methods
The KhmerLunarDate object provides the following methods:
getDayOfWeek()
Returns the Khmer day of the week.
$lunarDate->getDayOfWeek(); // α’αΆαα·ααα, α
αααα, α’ααααΆα, αα»α, αααα ααααα·α, αα»ααα, αα
αα
getLunarDay()
Returns the lunar day with moon status (ααΎα for waxing, ααα for waning).
$lunarDate->getLunarDay(); // α‘ααΎα, α’ααΎα, α‘α€ααα
, etc.
getLunarMonth()
Returns the Khmer lunar month name.
$lunarDate->getLunarMonth(); // α
αααα, αα·ααΆα, ααααα, α’αΆααΆα, etc.
getLunarZodiac()
Returns the animal year (Khmer zodiac).
$lunarDate->getLunarZodiac(); // ααΌα, ααααΌα, ααΆα, ααα, ααα, ααααΆαα, αααΈ, ααα, αα, αααΆ, α
, αα»α
getLunarEra()
Returns the era year (10-year cycle).
$lunarDate->getLunarEra(); // ααααΈααα, α
ααααΆααα, αααα
ααα, etc.
getLunarYear()
Returns the Buddhist Era year in Khmer numerals.
$lunarDate->getLunarYear(); // α’α₯α¦α₯, α’α₯α¦α¦, etc.
toString()
Returns the complete lunar date as a formatted string.
$lunarDate->toString();
// ααααα
αααα α€ ααα
ααααααΆααΆα ααααΆαααααΌα ααααΈααα αα»αααααααΆα α’α₯α¦α₯
Caching
The Chhankitek package implements automatic caching to improve performance:
- Each converted date is cached automatically
- Cache duration: 365 days
- Uses Laravel's default cache driver (configured in your application)
- No manual cache management required
The package leverages Laravel's cache system, so conversions are cached efficiently whether you're using Redis, Memcached, or the file cache driver.
Requirements
- PHP: ^8.2
- Laravel: ^10.0 | ^11.0 | ^12.0
- Carbon: CarbonImmutable (included with Laravel)
Examples
Example 1: Convert Current Date
$today = toLunarDate(
CarbonImmutable::now()->setTimezone('Asia/Phnom_Penh')
);
echo "Day: " . $today->getDayOfWeek();
echo "Lunar Day: " . $today->getLunarDay();
echo "Month: " . $today->getLunarMonth();
echo "Year: " . $today->getLunarYear();
Example 2: Convert Specific Date
$date = CarbonImmutable::createFromFormat(
'Y-m-d',
'2024-04-14'
)->setTimezone('Asia/Phnom_Penh');
$lunarDate = toLunarDate($date);
echo $lunarDate->toString();
Example 3: Display in Blade Template
// In Controller
public function index()
{
$lunarDate = toLunarDate(
CarbonImmutable::now()->setTimezone('Asia/Phnom_Penh')
);
return view('welcome', compact('lunarDate'));
}
// In Blade (welcome.blade.php)
<div>
<h1>{{ $lunarDate->toString() }}</h1>
<p>Animal Year: {{ $lunarDate->getLunarZodiac() }}</p>
</div>
Example 4: Using in API
Route::get('/api/lunar-date', function (Request $request) {
$date = $request->input('date')
? CarbonImmutable::parse($request->input('date'))
: CarbonImmutable::now();
$date = $date->setTimezone('Asia/Phnom_Penh');
$lunarDate = toLunarDate($date);
return response()->json([
'gregorian' => $date->format('Y-m-d'),
'lunar' => [
'full' => $lunarDate->toString(),
'day_of_week' => $lunarDate->getDayOfWeek(),
'lunar_day' => $lunarDate->getLunarDay(),
'month' => $lunarDate->getLunarMonth(),
'zodiac' => $lunarDate->getLunarZodiac(),
'era' => $lunarDate->getLunarEra(),
'year' => $lunarDate->getLunarYear(),
],
]);
});
Understanding the Khmer Calendar
Lunar Months
The Khmer lunar calendar has 12 standard months. In leap years, an additional month (ααααΆααΆα) is added:
- αα·ααα·α (Mikasar)
- αα»ααα (Boss)
- ααΆα (Meak)
- ααααα»α (Phalguna)
- α αααα (Chaet)
- αα·ααΆα (Pisak)
- ααααα (Chet)
- α’αΆααΆα (Asath)
- ααααΆααα (Srapon)
- αααααα (Phutrobot)
- α’αααα»α (Assoch)
- ααααα·α (Kadek)
Lunar Days
Each lunar month is divided into two phases:
- ααΎα (Kaet): Waxing moon (days 1-15)
- ααα (Roch): Waning moon (days 1-15)
Zodiac Animals (12-year cycle)
Leap Years
The Khmer calendar has two types of leap years:
- Leap Month Year (Adhikameas): An extra 13th month (ααααΆααΆα) is added, making the year 384 days long
- Leap Day Year: An extra day is added to the month of ααααα, making the year 355 days long
- Regular Year: 354 days with 12 months
Learn More: Visit Khmer Calendar for detailed information about the Khmer calendar system.
API Reference
HasChhankitek Trait
chhankiteck(CarbonImmutable $target): KhmerLunarDate
Converts a Gregorian date to Khmer lunar date format.
Helper Function
toLunarDate(CarbonImmutable $target): KhmerLunarDate
Global helper function for date conversion.
KhmerLunarDate Methods
| Method | Return Type | Description |
|---|---|---|
| getDayOfWeek() | string | Khmer day of week |
| getLunarDay() | string | Lunar day with moon status |
| getLunarMonth() | string | Khmer lunar month name |
| getLunarZodiac() | string | Animal year (zodiac) |
| getLunarEra() | string | Era year (10-year cycle) |
| getLunarYear() | string | Buddhist Era year (Khmer numerals) |
| toString() | string | Complete formatted date string |
Contributing
Contributions are welcome! Please see the CONTRIBUTING.md file for details.
Found a security issue? Please email mabhelitc@gmail.com instead of using the issue tracker.