briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.47k stars 1.28k forks source link

Other calendars #624

Closed salarmehr closed 6 years ago

salarmehr commented 8 years ago

Similar to Cabon, IntlDateTime is an extended version of php 5 DateTime class. Additionally it supports multiple calendars and locales provided by ICU project.

Its code can be reused in Carbon and add Solar Hijri, Hebrew, Hijri and other calendar types provided by ICU your great work become applicable in the reset of world. I particularly need Solar Hijri for the countries that does not use Gregorian calendar at all.

lorenzo commented 8 years ago

@salarmehr I have solved this for my own projects by using this package, which contains a Time class that works the same as Carbon but can also handle calendars and locale formats when formatting the date.

salarmehr commented 8 years ago

@lorenzo Thanks for the link. I hope to see this feature in the carbon.

May you provide an example code from that project for converting calendars?

lorenzo commented 8 years ago

Yes, it looks like this:

$time = new Time('2010-01-14 13:59:28 2010');
echo $time->i18nFormat(IntlDateFormatter::FULL, null, 'ps-IR@calendar=persian');
// 'پنجشنبه د  ۱۳۸۸ د مرغومی ۲۴ ۱۳:۵۹:۲۸ (GMT)';
salarmehr commented 8 years ago

@lorenzo Thanks, can it also get a non-Gregorian date and convert it to another type?

Also here is your code using IntlDateTime class:

    $date = new IntlDateTime($time, 'Asia/Tehran');
    $date->setCalendar('persian');
    echo $date->format('yyyy/MM/dd HH:mm');

Its api and code coverage is very well.

lorenzo commented 8 years ago

Yes, there is Time::parse($nonGregorianDate, $localeOptions)

soap commented 8 years ago

I think in Carbon, it is better if we can override toString or other similar in localization.

For example, in Thai I have to convert to BE calendar by adding 543 to year when display to user.

p3ym4n commented 7 years ago

currently I've done this feature for jalali date. so the methods to convert georgian and jalali date to each other have been implemented. but i'm struggling with carbon to put the convertors as a facade in carbon getter & setters. you know its very hard , all the properties currently passed through getters and setters even inside carbon class. I hope it ends soon to notify you. 😃

p3ym4n commented 7 years ago

Finally i did it. This jalali date package have carbon included. https://github.com/p3ym4n/jdate

aliabdzad commented 7 years ago

@p3ym4n thanks . i hope this works fine ;)

kylekatarnls commented 6 years ago

To avoid mix too many responsibilities, I find it preferable that one date library should handle one calendar and so using an other library seems the right think to do rather than adding calendars in Carbon.