如何在PHP中将默认日期设置为波斯日期?

5

如何在PHP中将默认日期设置为波斯语日期?

假设我使用echo函数date('Y-m-d'),那么它将显示2018-03-05,但我想要1396-12-14这个波斯日期。


如果可以通过时区完成,请提出建议。 - Ashutosh Singh Lodhi
尝试使用这个 PHP 类:https://www.phpclasses.org/package/4852-PHP-Convert-and-format-dates-of-the-Persian-calendar.html - Ouatataz
这可能会对你有所帮助? https://www.codeproject.com/articles/28380/persian-calendar-in-php - Edwin Krause
2个回答

16
请检查:http://php.net/manual/zh/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;
}

谢谢大家的帮助,但是这并没有帮到我。我想要的是,如果我输出 date('Y-m-d') ,那么它应该返回 1396-12-15 而不是 2018-03-06。 - Ashutosh Singh Lodhi
1
有没有更好的方法将数字转换为英文? - M at
3
使用 "en_US@calendar=persian" 可以获得英文数字。 - M at
3
如果使用en_US而不是fa_IR,那么您就不需要使用convert_to_number函数。 - Nabi K.A.Z.
这个函数只是转换日期,不返回时间!我想要日期和时间都显示,格式为"Y-m-d h:i"。 - Nadia

1

在 PHP 中使用波斯语日期格式(Finglish):

<?php

// date_default_timezone_set('Asia/Tehran');

$now = new DateTime();

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

// It is now: 1401-Bahman-20
echo 'It is now: ' . $formatter->format($now) ."<br />"; 
?>

如果您想更改日期格式,请参考以下链接:

https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接