如何编写一个函数,通过用它自己的语言调用它来获取每个区域的日期? [重复]

问题描述 投票:0回答:2

我在 wordpress 中编写了一些 php 以从格里高利获取波斯语和阿拉伯语日期。 我看到这个: 格式化 DateTime 对象,尊重 Locale::getDefault()

我想要一个函数,每次调用函数时只需更改区域和时区即可获取波斯语和阿拉伯语日期

php wordpress calendar hijri
2个回答
1
投票

在 PHP 中,您不能使用英语以外的具有标准

date
/
DateTime
结构的语言。执行此操作的唯一方法是使用
setlocale()
设置区域设置并使用
strfttime()
函数...但是该函数现已弃用,转而使用 INTL/ICU 扩展的
IntlDateFormatter
班级:

function getFormattedDateIntl(
    ?\DateTime $date = null,
    ?string $locale = null,
    ?DateTimeZone $timezone = null,
    string $dateFormat
) {
    $date = $date ?? new \DateTime();
    $locale = $locale ?? \Locale::getDefault();
    $formatter = new \IntlDateFormatter(
        $locale,
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        $timezone,
        IntlDateFormatter::TRADITIONAL,
        $dateFormat
    );
    return $formatter->format($date);
}

function getWeekdayIntl(
    ?\DateTime $date = null,
    ?string $locale = null,
    ?DateTimeZone $timezone = null
) {
    return getFormattedDateIntl($date, $locale, $timezone, 'eeee');
}

$islamicDateRight = getFormattedDateIntl(
    new DateTime(),
    'ar@calendar=islamic-civil',
    new \DateTimeZone('Asia/Tehran'),
    'eeee dd MMMM'
);

0
投票
function convert_day_to_arabic($day) {
    $days = array(
        "Saturday" => "السبت",
        "Sunday" => "الأحد",
        "Monday" => "الإثنين",
        "Tuesday" => "الثلاثاء",
        "Wednesday" => "الأربعاء",
        "Thursday" => "الخميس",
        "Friday" => "الجمعة"
    );

    echo isset($days[$day]) ? $days[$day] : $day;
}
© www.soinside.com 2019 - 2024. All rights reserved.