skip to Main Content

I’m in wordpress and write some php to get today’s day of the week in arabic language like الخمیس for thursday.

this is my code so far

                        function getWeekday() {
                            $dateName = date('l');
                            return date('w', strtotime($dateName));
                        }
                        
                        echo getWeekday(); //today is monday and it echo's 2

if you know better way of codding to achive this is admirable.

2

Answers


  1. function convert_day_to_arabic($day) {
        $days = array(
            "Saturday" => "السبت",
            "Sunday" => "الأحد",
            "Monday" => "الإثنين",
            "Tuesday" => "الثلاثاء",
            "Wednesday" => "الأربعاء",
            "Thursday" => "الخميس",
            "Friday" => "الجمعة"
        );
    
        echo isset($days[$day]) ? $days[$day] : $day;
    }
    
    Login or Signup to reply.
  2. You can’t use languages other than English with the standard date/DateTime constructs in PHP. The only way to do this was to set the locale using setlocale() and use the strfttime() function… however that function is now deprecated in favor of using the INTL/ICU extension’s IntlDateFormatter class:

    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'
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search