A library like Carbon but for Iranian (Jalali) calendar
Via Composer
$ composer require alitnk/radon
Just like when you would do now()
and get a Carbon instance, you can do jnow()
and get a Radon instance for the current time.
To make a new instance based on the given datetime, you can use radon($datetimeString)
like:
$r = radon('1401-12-28 06:15')
and get a new instance for that datetime
To convert Carbon instances to Radon instances, do:
$carbonInstance->toJalali(); // returns Radon instance and converts the date from gregorian to jalali
To do the vice versa (Radon to Carbon), do:
$radonInstance->toGregorian(); // returns Carbon instance and converts the date from jalali to gregorian
You can use most of the carbon's methods on a Radon instance. e.g. $radonInstance->diffForHumans()
, $radonInstance->addDay(10)
, $radonInstance->setDay(1)
Radon introduces new querying features for Jalali dates. Available methods are:
Note that this method is also available on Collection
objects.
Example:
Product::whereBetweenJalali('created_at', [radon('1398-10-12'), radon('1398-11-12')])->get();
This method is like orWhereBetween
but for Jalali dates.
Comment::whereDateJalali('created_at', radon('1400-01-01'))->get(); // Gets all the comments for first day of 1400
Compares the day
Comment::whereDayJalali('created_at', 31); // Gets all comments for 31th
Compares the month
Comment::whereMonthJalali('created_at', 1); // Gets all of Farvardin's comments
Compares the year
Comment::whereYearJalali('created_at', 1400); // Gets all of 1400's comments
You can use the Radon cast to make the conversion process easier. To use the cast, add the cast to your field in the respective model:
use Wama\Radon\Casts\JalaliDatetime;
class Product extends Model {
protected $casts = [
'sales_ends_at' => JalaliDatetime::class,
'updated_at' => JalaliDatetime::class,
'created_at' => JalaliDatetime::class,
];
}
and get a Radon instance every time you get the field.
$product->sales_ends_at // returns a Radon instance
you can also update the model without having to worry about the datetime conversions:
$product->update([
'sales_ends_at' => radon('1401-01-14 23:00'), // Sales will end at Farvardin 14th
]);
or
$product->sales_ends_at->addMonth();
$product->save();
Radon tries to support most of the Carbon methods, but keep in mind that some of the Carbon methods are not supported yet and might be added in the next versions. This package uses Verta under the hood and therefore, expanding some functionalities might demand on the expansion of Verta.
Please see the changelog for more information on what has changed recently.
$ composer test
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
license. Please see the license file for more information.