Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default date to Persian date in PHP?

How to set default date to Persian date in PHP?

Say, if I echo this function date('Y-m-d') then it will show 2018-03-05 but I want 1396-12-14 the Persian date. Please help

like image 518
Ashutosh Singh Lodhi Avatar asked Mar 05 '18 12:03

Ashutosh Singh Lodhi


1 Answers

Please check: http://php.net/manual/en/intldateformatter.create.php

<?php

// date_default_timezone_set('Asia/Tehran');

$now = new DateTime();

$formatter = new IntlDateFormatter(
                "fa_IR@calendar=persian", 
                IntlDateFormatter::FULL, 
                    IntlDateFormatter::FULL, 
                'Asia/Tehran', 
                IntlDateFormatter::TRADITIONAL, 
                "yyyy-MM-dd");

// It is now: 1396-12-14
echo 'It is now: ' . convert_to_number($formatter->format($now)) ."<br />"; 

// It is now: ۱۳۹۶-۱۲-۱۴
echo 'It is now: ' . $formatter->format($now) ."<br />"; 


// convert to number
function convert_to_number($string) {
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠'];

    $num = range(0, 9);
    $convertedPersianNums = str_replace($persian, $num, $string);
    $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums);

    return $englishNumbersOnly;
}
like image 177
B.Kocaman Avatar answered Nov 15 '22 02:11

B.Kocaman