A Simple way to work with Jalali date in Laravel using Carbon and jdf.php

Hossein Pourbahrami
3 min readFeb 12, 2021

As you know, Laravel uses powerful Carbon package to handle date and time by default, and automatically converts model date attributes to Carbon instance. This makes the programmer relatively free to work with date attributes in Laravel models and enables him/her to use the dozens of methods provided by carbon.

But this ease of use just stays only as long as you want to use the time difference or back and forth time or Gregorian date, and when it comes to Jalali date, no more work has been done by Carbon. Although Carbon has possibility of localization and you can give fa_IR as a locale parameter to it, But this only translates Carbon textual outputs into Persian, and the type of date remains Gregorian.

Now with these descriptions, how can the Jalali date in Laravel be used? Obviously there is no definitive answer to this question and every programmer will find a way to do it according to the conditions of his/her project. Some recommend using the Morilog/jalali package, some still use the jdf.php library (either as a Helper function or by converting it to a class) and recently many have been using the Verta package and of course dozens of solutions that everyone may or may not use according to their choice.

But what is unacceptable to me about the above methods — apart from the fact that, in my opinion, in practice they are in conflict with Laravel’s spirit to achieve the result with the least amount of code — is that in all of them Carbon is bypassed and practically abandoned. For example, the Verta package is very complete and powerful, and especially has good tools for validation, but where Carbon exists, why we should reinvent the wheel?

My suggestion is to extend Carbon to add support of Jalali calendar to it, instead of abandoning it. As you know, carbon has a method called macro which, like other macro methods, has the ability to add custom methods and attributes to the class. On the other hand, we have the jdf.php library, which is one of the simplest tools for working with Jalali date in the world of php, and if you have not worked with it before, you can read all its documents in less than 10 minutes . So what if we combine the two:

We simply added two methods, jdate and jmktime, to the Carbon. the first to print the Jalali date output and the second to create a Carbon instance of the Jalali date, also we kept Carbon and its possibilities.

Now we just have to make sure that the code is executed at the beginning of the program so that these methods can be used everywhere in the project. To do this, you can put the code in the boot method of the AppServiceProvider, or even to clean up the work, create a new provider service called CarbonServiceProvider and put the code in its boot method:

Obviously I named the new methods jdate and jmktime and changed the order of the input arguments, you can specify any name you want (like formatJalali and createJalali) and any argument order you want. Obviously, using the Carbon macro method, you can add as many custom methods and attributes as you want to Carbon, and of course, you can use other tools like Verta itself inside the macro to convert the date instead of the jdf.php I used.

یه راه ساده برای کار با تاریخ شمسی در لاراول با استفاده از Carbon و jdf.php

--

--