How to convert JalaliDate to GregorianDate in yii2?

I want to convert jalali to gregorian. When entering data, the jalali date in the field of view is accepted:

<?= $form->field($model, 'start_date')->widget(jDate\DatePicker::className())->textInput() ?>

in the controller:

 $jstartdate = $model->start_date; // string(10) "1395/06/15" 

Now I want to save in the database in the gregorian format "2016-09-05". What function in jalali class works? I used many functions, for example createFromFormator toGregorian.. but I did not get the result.

+4
source share
2 answers

Since Yii 2.0.7:

Yii::$app->formatter->locale = 'fa_IR@calendar=persian';
Yii::$app->formatter->calendar = \IntlDateFormatter::TRADITIONAL;
Yii::$app->formatter->timeZone = 'UTC';
$value = 1451606400; // Fri, 01 Jan 2016 00:00:00 (UTC)
echo Yii::$app->formatter->asDate($value, 'php:Y');
// outputs "۱۳۹۴"
+3
source

intl, , php , 5.4. , IntlCalendar :

$date = IntlCalendar::createInstance(
    'Asia/Tehran',
    'fa_IR@calendar=persian'
);

:

$date->set(1395, 5, 15, 19, 17, 11); // Notice that month number begin from 0 not 1.

IntlDateFormatter :

$intlDateFormatter = new IntlDateFormatter(
    "en_US", // string $locale
    IntlDateFormatter::FULL, // int $datetype
    IntlDateFormatter::FULL, // int $timetype
    'Asia/Tehran', // mixed $timezone
    IntlDateFormatter::GREGORIAN, // mixed $calendar
    'yyyy/MM/dd HH:mm:ss' // string $pattern
);

toDateTime :

var_dump($intlDateFormatter->format($date));
// /srv/http/test/DateTime.php:29:
// string(19) "2016/09/05 19:17:11"

PS: , : https://github.com/meysampg/intldate, date , IntlDateBehavior.

+2

Source: https://habr.com/ru/post/1653674/


All Articles