alitnk / radon
:package_description
Requires
- hekmatinasser/verta: ^1.12
- illuminate/support: ~7|~8
Requires (Dev)
- orchestra/testbench: ~5|~6
- phpunit/phpunit: ~9.0
README
A library like Carbon but for Iranian (Jalali) calendar
Installation
Via Composer
$ composer require alitnk/radon
Usage
Create new instance
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
Conversions
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
Methods
You can use most of the carbon's methods on a Radon instance. e.g. $radonInstance->diffForHumans()
, $radonInstance->addDay(10)
, $radonInstance->setDay(1)
Eloquent / Querying
Radon introduces new querying features for Jalali dates. Available methods are:
whereBetweenJalali
Note that this method is also available on Collection
objects.
Example:
Product::whereBetweenJalali('created_at', [radon('1398-10-12'), radon('1398-11-12')])->get();
orWhereBetweenJalali
This method is like orWhereBetween
but for Jalali dates.
whereDateJalali
Comment::whereDateJalali('created_at', radon('1400-01-01'))->get(); // Gets all the comments for first day of 1400
whereDayJalali
Compares the day
Comment::whereDayJalali('created_at', 31); // Gets all comments for 31th
whereMonthJalali
Compares the month
Comment::whereMonthJalali('created_at', 1); // Gets all of Farvardin's comments
whereYearJalali
Compares the year
Comment::whereYearJalali('created_at', 1400); // Gets all of 1400's comments
Casts
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();
Expansion
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.
Change log
Please see the changelog for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
license. Please see the license file for more information.