Display dates in Arabic

こ雲淡風輕ζ 提交于 2020-01-01 05:27:06

问题


Here's my code:

setlocale( LC_ALL,'ar' ); 
echo strftime( '%e %b, %Y', strtotime( '2011-10-25' ));

Output:

25 Sep, 2011

Why is it not displaying the arabic date? Am I using strftime incorrectly?


回答1:


AFAIK setlocale won't actually do any language translation for you but rather affects things like the formatting and comparator functionality. If you want localisation then you could try using IntlDateFormatter which may give you what you need.

Updated: You could also try Zend_Date as suggested in this question if PHP 5.3 isn't an option for you.




回答2:


Here you can print the Arabic PHP Date :

Create a file called arabicdate.php and place this function inside it :

function ArabicDate() {
    $months = array("Jan" => "يناير", "Feb" => "فبراير", "Mar" => "مارس", "Apr" => "أبريل", "May" => "مايو", "Jun" => "يونيو", "Jul" => "يوليو", "Aug" => "أغسطس", "Sep" => "سبتمبر", "Oct" => "أكتوبر", "Nov" => "نوفمبر", "Dec" => "ديسمبر");
    $your_date = date('y-m-d'); // The Current Date
    $en_month = date("M", strtotime($your_date));
    foreach ($months as $en => $ar) {
        if ($en == $en_month) { $ar_month = $ar; }
    }

    $find = array ("Sat", "Sun", "Mon", "Tue", "Wed" , "Thu", "Fri");
    $replace = array ("السبت", "الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة");
    $ar_day_format = date('D'); // The Current Day
    $ar_day = str_replace($find, $replace, $ar_day_format);

    header('Content-Type: text/html; charset=utf-8');
    $standard = array("0","1","2","3","4","5","6","7","8","9");
    $eastern_arabic_symbols = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩");
    $current_date = $ar_day.' '.date('d').' / '.$ar_month.' / '.date('Y');
    $arabic_date = str_replace($standard , $eastern_arabic_symbols , $current_date);

    return $arabic_date;
}

Now include this file in your page :

include 'arabicdate.php';

Then you can print the Arabic PHP Date :

echo ArabicDate();

Live Formatted Example :

http://ideone.com/MC0hou

Hope that helps.




回答3:


How about this:

function arabicDate($time)
{
    $months = ["Jan" => "يناير", "Feb" => "فبراير", "Mar" => "مارس", "Apr" => "أبريل", "May" => "مايو", "Jun" => "يونيو", "Jul" => "يوليو", "Aug" => "أغسطس", "Sep" => "سبتمبر", "Oct" => "أكتوبر", "Nov" => "نوفمبر", "Dec" => "ديسمبر"];
    $days = ["Sat" => "السبت", "Sun" => "الأحد", "Mon" => "الإثنين", "Tue" => "الثلاثاء", "Wed" => "الأربعاء", "Thu" => "الخميس", "Fri" => "الجمعة"];
    $am_pm = ['AM' => 'صباحاً', 'PM' => 'مساءً'];

    $day = $days[date('D', $time)];
    $month = $months[date('M', $time)];
    $am_pm = $am_pm[date('A', $time)];
    $date = $day . ' ' . date('d', $time) . ' - ' . $month . ' - ' . date('Y', $time) . '   ' . date('h:i', $time) . ' ' . $am_pm;
    $numbers_ar = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
    $numbers_en = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

    return str_replace($numbers_en, $numbers_ar, $date);
}

Note: the parameter ($time) should be Unix timestamp.




回答4:


Inspired by Amr SubZero's answer above:

If anybody else needed this, these two functions displays post date and time in arabic for a wordpress website:

DATE:

functions.php

function single_post_arabic_date($postdate_d,$postdate_d2,$postdate_m,$postdate_y) {
    $months = array("Jan" => "يناير", "Feb" => "فبراير", "Mar" => "مارس", "Apr" => "أبريل", "May" => "مايو", "Jun" => "يونيو", "Jul" => "يوليو", "Aug" => "أغسطس", "Sep" => "سبتمبر", "Oct" => "أكتوبر", "Nov" => "نوفمبر", "Dec" => "ديسمبر");
    $en_month = $postdate_m;
    foreach ($months as $en => $ar) {
        if ($en == $en_month) { $ar_month = $ar; }
    }

    $find = array ("Sat", "Sun", "Mon", "Tue", "Wed" , "Thu", "Fri");
    $replace = array ("السبت", "الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة");
    $ar_day_format = $postdate_d2;
    $ar_day = str_replace($find, $replace, $ar_day_format);

    header('Content-Type: text/html; charset=utf-8');
    $standard = array("0","1","2","3","4","5","6","7","8","9");
    $eastern_arabic_symbols = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩");
    $post_date = $ar_day.' '.$postdate_d.' '.$ar_month.' '.$postdate_y;
    $arabic_date = str_replace($standard , $eastern_arabic_symbols , $post_date);

    return $arabic_date;
}

Inside the loop:

<date>
<?php
  $postdate_d = get_the_date('d');
  $postdate_d2 = get_the_date('D');
  $postdate_m = get_the_date('M');
  $postdate_y = get_the_date('Y');                                

 echo single_post_arabic_date($postdate_d,$postdate_d2, $postdate_m, $postdate_y);
?>
</date>

TIME:

functions.php

function single_post_arabic_time($posttime_h, $posttime_i, $posttime_a) {

    $ampm = array("AM", "PM");
    $ampmreplace = array("ق.ظ", "ب.ظ");
    $ar_ampm = str_replace($ampm, $ampmreplace, $posttime_a);

    header('Content-Type: text/html; charset=utf-8');
    $standardletters = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
    $eastern_arabic_letters = array("٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩");
    $post_time = $posttime_h . ':' . $posttime_i." ".$ar_ampm;
    $arabic_time = str_replace($standardletters, $eastern_arabic_letters, $post_time);

    return $arabic_time;
}

Inside the loop:

<span>الساعة </span>
<time>
<?php
  $posttime_h = get_the_date('h');
  $posttime_i = get_the_date('i');
  $posttime_s = get_the_date('d');
  $posttime_a = get_the_date('A');

echo single_post_arabic_time($posttime_h,$posttime_i,$posttime_a);
?>
</time>



回答5:


Have you run

locale -a

and verified that your system has a locale called "ar"? It might be called something more specific, e.g. "ar_AR.utf8"... If you need to support Arabic locale spelled differently in multiple systems, you may pass an array to setlocale(). The first locale name in that array that the system supports will be used.




回答6:


I use this javascript function if i can help:

<script type='text/javascript'>

navig = navigator.appName;

versn = parseInt(navigator.appVersion);

if ( (navig == "Netscape" && versn >= 3) || (navig == "Microsoft     Internet Explorer" && versn >= 4)) 
info = "true";

else info = "false";

function Ar_Date() {

   if (info == "true") {

    var info3 = new Date();
    var info4=info3.getDay();
    var info5=info3.getMonth();
    var info6=info3.getDate();
    var info7=info3.getFullYear();
    var info8 = new Array('&#1604;&#1571;&#1581;&#1583;','&#1575;&#1604;&#1573;&#1579;&#1606;&#1610;&#1606;','&#1575;&#1604;&#1579;&#1604;&#1575;&#1579;&#1575;&#1569;','&#1575;&#1604;&#1571;&#1585;&#1576;&#1593;&#1575;&#1569;','&#1575;&#1604;&#1582;&#1605;&#1610;&#1587;','&#1575;&#1604;&#1580;&#1605;&#1593;&#1577;','&#1575;&#1604;&#1587;&#1576;&#1578;');
    var info9 = info8[info4];
    var info10 = new Array('&#1580;&#1575;&#1606;&#1601;&#1610;','&#1601;&#1610;&#1601;&#1585;&#1610;','&#1605;&#1575;&#1585;&#1587;','&#1571;&#1601;&#1585;&#1610;&#1604;','&#1605;&#1575;&#1610;','&#1580;&#1608;&#1575;&#1606;','&#1580;&#1608;&#1610;&#1604;&#1610;&#1577;','&#1571;&#1608;&#1578;','&#1587;&#1576;&#1578;&#1605;&#1576;&#1585;','&#1571;&#1603;&#1578;&#1608;&#1576;&#1585;','&#1606;&#1608;&#1601;&#1605;&#1576;&#1585;','&#1583;&#1610;&#1587;&#1605;&#1576;&#1585;');
    var info11 = info10[info5];
    var info12=info9+'&#1548; '+info6+' '+info11+' '+info7;
    var info12=info9+'&#1548; '+info6+' '+info11;

    document.write(info12);

   }

}

</script>



回答7:


function single_post_arabic_date($postdate_d,$postdate_d2,$postdate_m,$postdate_y) {
    $months = array("01" => "يناير", "02" => "فبراير", "03" => "مارس", "04" => "أبريل", "05" => "مايو", "06" => "يونيو", "07" => "يوليو", "08" => "أغسطس", "09" => "سبتمبر", "10" => "أكتوبر", "11" => "نوفمبر", "12" => "ديسمبر");


       $ar_month =months[$postdate_m]; 


    $find = array ("Sat", "Sun", "Mon", "Tue", "Wed" , "Thu", "Fri");
    $replace = array ("السبت", "الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة");
    $ar_day_format = $postdate_d2;
    $ar_day = str_replace($find, $replace, $ar_day_format);

    header('Content-Type: text/html; charset=utf-8');
    $standard = array("0","1","2","3","4","5","6","7","8","9");
    $eastern_arabic_symbols = array("٠","١","٢","٣","٤","٥","٦","٧","٨","٩");
    $post_date = $ar_day.' '.$postdate_d.' '.$ar_month.' '.$postdate_y;
    $arabic_date = str_replace($standard , $eastern_arabic_symbols , $post_date);

    return $arabic_date;
}

this is just improve function

<?php
      $postdate_d = get_the_date('d'); 
     $postdate_d2 = get_the_date('D');
      $postdate_m = get_the_date('m');
      $postdate_y = get_the_date('Y');                                

     echo single_post_arabic_date($postdate_d,$postdate_d2, $postdate_m, $postdate_y);
?>



回答8:


Does this work for you:

setlocale(LC_ALL,'ar');
echo strftime('%A %d %B %Y'); 

Hope it helps



来源:https://stackoverflow.com/questions/8166907/display-dates-in-arabic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!