yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

jalali date in formatter #8687

Closed hooman-mirghasemi closed 8 years ago

hooman-mirghasemi commented 9 years ago

if it's possible for you add an ablility to framework, to have jalali date format and when we set some property in config, like (dateType => 'jalali',) or (dateType => 'gregorian ') by depending on this property in config, formatter works.

hear is the best jalalidate vendor in php that tested in all iranian app and it's working currectly: http://jdf.scr.ir/

samdark commented 9 years ago

Have you tried setting formatter's $locale to fa_IR@calendar= persian?

hooman-mirghasemi commented 9 years ago

yes I test it. it is only done: feb 2015 to: فبریوری 2015 all thing it's done is only translating mount name and change number formatting, not real change calender if it realy change calender should change that example to: اردیبهشت 1394

I have an repo for jalali ext based on jdf it's: https://github.com/hooman-pro/yii2-jdf

and my mean is done some work on the base of framework for have this ability on core

hooman-mirghasemi commented 9 years ago

I done it in my project by extenting formmatter like down:

<?php namespace common\component;

use Yii; use hoomanMirghasemi\jdf\Jdf;

class MyFormatter extends \yii\i18n\Formatter {

const DATE_TYPE__GREGORIAN = 'gregorian';
const DATE_TYPE__JALALI = 'jalali';

public $dateFormatJalali = 'Y F d';

public $timeFormatJalali = 'Y F d H:i:s';

public $dateType = 'gregorian';

public function asDate($value, $format = null, $jalaliFormat = null){
    if($this->dateType == self::DATE_TYPE__GREGORIAN){ 
        if ($format === null) {
            $format = $this->dateFormat;
        }
        return parent::asDate($value,$format);
    }elseif($this->dateType == self::DATE_TYPE__JALALI){
        if ($jalaliFormat === null) {
            $jalaliFormat = $this->dateFormatJalali;
        }
        return Jdf::jdate($jalaliFormat,$value);
    }
}

public function asDatetime($value, $format = null, $jalaliFormat = null){ 
    if($this->dateType == self::DATE_TYPE__GREGORIAN){
        if ($format === null) {
            $format = $this->datetimeFormat;
        }
        return parent::asDatetime($value,$format);
    }elseif($this->dateType == self::DATE_TYPE__JALALI){
        if ($jalaliFormat === null) {
            $jalaliFormat = $this->timeFormatJalali;
        }
        return Jdf::jdate($jalaliFormat,$value);
    }
}

}

and in my config -> components I can use it like: 'formatter' => [ 'class' => 'common\component\CoreFormatter', 'dateType'=>'gregorian', 'nullDisplay' => '(not set)', 'dateFormat' => 'medium', 'dateFormatJalali' => 'Y F d', 'timeFormatJalali' => 'Y F d H:i:s', 'datetimeFormat' => 'medium', 'booleanFormat' => [ '', '' ]

    ],

Do You want to I done it, in forrmatter class and send by pull request?

cebe commented 9 years ago

what is Jdf::jdate ?

cebe commented 9 years ago

yes, you can send a pull request.

hooman-mirghasemi commented 9 years ago

jdf means : jalali date format all functions that are in php for date are changed in jdf with j prefix, like: jdate for date and jmktime for mktime, for done it I should add jdf class and some changes on yii/i18/formmater.

a question: where is the best palce for jdf class in yii folders and namespace?

cebe commented 9 years ago

can you show how the jdf class looks like?

hooman-mirghasemi commented 9 years ago

yes this is hear: https://github.com/hooman-pro/yii2-jdf/edit/master/Jdf.php a static class have all date fuontinality for jalali calendar

hooman-mirghasemi commented 9 years ago

excuse me link is: https://github.com/hooman-pro/yii2-jdf/blob/master/Jdf.php

do you see it? where is the best place and namespace?

hooman-mirghasemi commented 9 years ago

I don it. how is it?

armpogart commented 9 years ago

As a reference another one https://github.com/sallar/jDateTime

samdark commented 9 years ago

Hmm... need to confirm that it's not possible using intl before adding more classes.

hooman-mirghasemi commented 9 years ago

@samdark : i can't understand your means, more description please.

@armpogart : Jdf is the best jalali date library and it tested many many time. I choose it for that's trust between iranian and pepole use jalali calendar

samdark commented 9 years ago

There's http://php.net/manual/en/book.intl.php which we rely on and if it can handle jalali calendar, I'd prefer it to any library written in PHP.

cebe commented 9 years ago

ICU seems to have support for "@calendar=persian", not sure if there is anything needed by the intl extension to enable it.

for reference:

hooman-mirghasemi commented 9 years ago

I could not. set it by intl class. all calendar i test theme in formatter local, and call asDate(time()); as down: @calendar=taiwan 2015 Month6 9 @calendar=persian ۹ ژوئن ۲۰۱۵ م. @calendar=islamic ٩ يونيو، ٢٠١٥ م

. . . and so on. but now in jalali is: 1393/06/09 in islamic or ghamari calendar is: الثلاثاء - ٢١ شعبان ١٤٣٦

have good job.

z-avanes commented 8 years ago

hi i have same problem we can use intl for solve problem ( use @calendar=persian for locale param ) but we have small fraction in IntlDateFormatter class define in formatDateTimeValue function we need define calender argument for example :

$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $timeZone, IntlDateFormatter::TRADITIONAL, $format);
cebe commented 8 years ago

@z-avanes thanks for the hint! I have added a calendar property to the formatter, see #10517. could you check the test and verify it works as expected? I'd also love to include more locales in the test so please provide information on calendars you know.