add syntax sugar for Carbon conversions
This commit is contained in:
1 parent
3e1ef52534
commit
264b4488a6
5 files changed
+39
No files matched your search
@@ -118,6 +118,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
|
||||
<pre>
|
||||
$c = \Carbon::now();
|
||||
return verta($c); // 1395-12-09 15:05:56
|
||||
// or
|
||||
\Illuminate\Support\Carbon::now()->toJalali(); // 1395-12-09 15:05:56
|
||||
// or with laravel helper
|
||||
now()->toJalali(); // 1395-12-09 15:05:56
|
||||
// or in models
|
||||
return $model->created_at->toJalali();
|
||||
</pre>
|
||||
<p>To use the jalali datetime as a string, use the following command</p>
|
||||
<pre>
|
||||
@@ -165,6 +171,8 @@ $dt = verta()->datetime();
|
||||
<p>Verta to Carbon, use the following command</p>
|
||||
<pre>
|
||||
$c = Carbon::instance($v->datetime());
|
||||
// or
|
||||
$c = $v->toCarbon();
|
||||
</pre>
|
||||
<h3 id="property">Property</h3>
|
||||
<p>To get property, use the following command</p>
|
||||
|
||||
@@ -123,6 +123,12 @@ return new Verta($dt); // 1395-12-09 15:05:56
|
||||
<pre>
|
||||
$c = \Carbon::now();
|
||||
return verta($c); // 1395-12-09 15:05:56
|
||||
// or
|
||||
\Illuminate\Support\Carbon::now()->toJalali(); // 1395-12-09 15:05:56
|
||||
// or with laravel helper
|
||||
now()->toJalali(); // 1395-12-09 15:05:56
|
||||
// or in models
|
||||
return $model->created_at->toJalali();
|
||||
</pre>
|
||||
<p>برای استفاده از تاریخ شمسی به صورت رشته، از دستور زیر استفاده کنید</p>
|
||||
<pre>
|
||||
@@ -170,6 +176,8 @@ $dt = verta()->datetime();
|
||||
<p>برای تبدیل تاریخ از ورتا به کربن، از دستور زیر استفاده کنید</p>
|
||||
<pre>
|
||||
$c = Carbon::instance($v->datetime());
|
||||
// or
|
||||
$c = $v->toCarbon();
|
||||
</pre>
|
||||
<h3 id="property">خصوصیات</h3>
|
||||
<p>برای دریافت مقادیر خصوصیات، از دستورات زیر استفاده کنید</p>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Hekmatinasser\Verta\Laravel;
|
||||
|
||||
use Hekmatinasser\Verta\Verta;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
@@ -87,6 +88,10 @@ class VertaServiceProvider extends ServiceProvider
|
||||
$this->app->bind('verta', function ($app) {
|
||||
return new Verta();
|
||||
});
|
||||
|
||||
Carbon::macro('toJalali', function ($timezone = null) {
|
||||
return new Verta($this, $timezone);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Hekmatinasser\Verta\Traits;
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use Illuminate\Support\Carbon;
|
||||
use InvalidArgumentException;
|
||||
|
||||
trait Creator
|
||||
@@ -128,6 +129,16 @@ trait Creator
|
||||
return new DateTime(date('Y-m-d H:i:s', $this->getTimestamp()), $this->getTimezone());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Carbon instance from Verta
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
public function toCarbon()
|
||||
{
|
||||
return Carbon::instance($this->datetime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Verta instance from a DateTime one
|
||||
*
|
||||
|
||||
@@ -74,6 +74,13 @@ class CreatorTest extends TestCase
|
||||
$this->assertEquals('2019-01-01 10:20:11', $datetime);
|
||||
}
|
||||
|
||||
public function testToCarbon()
|
||||
{
|
||||
$datetime = verta('2019-01-01 10:20:11')->toCarbon()->format('Y-m-d H:i:s');
|
||||
|
||||
$this->assertEquals('2019-01-01 10:20:11', $datetime);
|
||||
}
|
||||
|
||||
public function testParse()
|
||||
{
|
||||
$datetime = Verta::parse('1397 دی 11 10:20:11')->formatGregorian('Y-m-d H:i:s');
|
||||
|
||||
Reference in new issue
Block a user