Display dates in Arabic
Asked Answered
L

12

8

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?

Lucullus answered 17/11, 2011 at 12:13 Comment(2)
What are you actually expecting?Jussive
I was expecting at least the month name in arabic.Lucullus
J
4

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.

Jussive answered 17/11, 2011 at 12:33 Comment(0)
E
28

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.

Emulsion answered 30/7, 2013 at 12:13 Comment(1)
aside from that the link is broken. This answer can be used in a various situations. Great snippetCircumrotate
D
7

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.

Davies answered 16/1, 2017 at 15:27 Comment(0)
J
4

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.

Jussive answered 17/11, 2011 at 12:33 Comment(0)
G
2

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>
Grus answered 5/10, 2015 at 11:24 Comment(0)
J
1

if all you're looking for is to print what day is today, then your question is easy...

Try this function.

<?php

function arDate(){
    $MONTHS =   array('كانون الثاني','شباط','آذار','نيسان','أيار','حزيران','تموز','آب','أيلول','تشرين الأول','تشرين الثاني','كانون الأول');
    $DAYS   =   array('الأحد','الاثنين','الثلاثاء','الأربعاء','الخميس','الجمعة','السبت');
    
    $dName      =   date("w");   // the number of the week-day ((from 0 to 6)). [0] for Sunday, [6] for Saturday //
    $dm         =   date("d");   // day of the month in numbers without leading zero; i.e.: 1, 2, 3... 28, 29, 30 //
    $mnth       =   date("n")-1; // number of the month ((from 1 to 12)) this is why we minus 1 from it so that it align with our $MONTHS array.;
    $yr         =   date('Y');   // four-digit year; eg.: 1981 //
    
    return $DAYS[$dName] . " " . $dm . " / " . $MONTHS[$mnth] . " / " . $yr;
}

$today = arDate();
echo $today; // الأحد 01 / آب / 2021 

?>

EXPLANATION: We first prepare two arrays with arabic names of both the days and months. Then we get four variables using the PHP built-in function date(). This function has lots of parameters to control its return. I'm here using the parameters that would give me numbers so that I use them as indexes in the $MONTHS[bla bla bla] and $DAYS[bla bla bla] vars. Finally, format your arabic date to your heart content!

have a look at PHP date() function in here

NOTE1: Do notice, please, that you can play with the arrangement of the days and months so that you don't need to minus one from your variables (-1) as I did above. Refer to the link of W3S and you would understand how to organize your arabic-name ARRAYS.

NOTE2: Also, notice please that I'm using the Classical Arabic names in my function and which are used in Syria only; they are not so well-known in the rest of the Arab-league states though they are the classical names for months in Arabic.

Jargonize answered 1/8, 2021 at 9:56 Comment(0)
C
0

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.

Crisis answered 29/11, 2011 at 10:43 Comment(0)
E
0

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>
Elva answered 28/4, 2016 at 12:15 Comment(0)
A
0
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);
?>
Amoreta answered 30/6, 2016 at 13:35 Comment(0)
Z
0

This should work:

setLocale(LC_ALL , 'ar_EG.utf-8');

If dates are still not displayed in Arabic, Then the arabic locale may not be installed on the system, To check it,connect using a terminal and type: locale -a, it would display the installed locales, if Arabic is not listed, you have to install it first and then it should work.

Zambrano answered 18/1, 2020 at 3:16 Comment(0)
N
0
  /**
   * Convert time string to arabic
   *@param string $time
   */
  public function arabicDate($time)
  {
      $en_data = ['January', 'Jan', 'Feburary', 'Feb', 'March', 'Mar',
      'April', 'Apr', 'May', 'June', 'Jun',
      'July', 'Jul', 'August', 'Aug', 'September', 'Sep',
      'October', 'Oct', 'November', 'Nov', 'December', 'Dec',
      'Satureday', 'Sat', 'Sunday', 'Sun', 'Monday', 'Mon',
      'Tuesday', 'Tue', 'Wednesday', 'Wed', 'Thursday', 'Thu', 'Friday', 'Fri',
      'AM', 'am', 'PM', 'pm'
      ];

      $ar_data = ['يناير', 'يناير', 'فبراير', 'فبراير', 'مارس', 'مارس',
      'أبريل', 'أبريل', 'مايو', 'مايو', 'يونيو', 'يونيو',
      'يوليو', 'يوليو', 'أغسطس', 'أغسطس', 'سبتمبر', 'سبتمبر',
      'أكتوبر', 'أكتوبر', 'نوفمبر', 'نوفمبر', 'ديسمبر', 'ديسمبر',
      'السبت', 'السبت', 'الأحد', 'الأحد', 'الإثنين', 'الإثنين',
      'الثلاثاء', 'الثلاثاء', 'الأربعاء', 'الأربعاء', 'الخميس', 'الخميس', 'الجمعة', 'الجمعة',
      'صباحاً', 'صباحاً', 'مساءً', 'مساءً'
      ];

      return str_replace($en_data, $ar_data, $time);
  }
Nipper answered 25/7, 2020 at 18:6 Comment(0)
R
0
<?php
$date = '21 Dec 22  14:13';
$date_time = new DateTime($date);

$formatter = new IntlDateFormatter('ar_DZ',);
print $formatter->format($date_time);

For more reference refer this link.

Ricardo answered 19/1, 2023 at 12:57 Comment(0)
K
-1

Does this work for you:

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

Hope it helps

Killion answered 17/11, 2011 at 12:20 Comment(1)
No, still getting english namesLucullus

© 2022 - 2025 — McMap. All rights reserved.