Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php date Internationalization

What I need is to get, date like, 15 march 2014 but in my language. Is there any easy built-in way to translate date function that whenever I use

date("j F Y");

I get something like 15 march 2014 in my language?

BTW I need Azerbaijani translation. Maybe php already has built-in date-time languages.

What I've done is:

  1. Executed sudo locale-gen az_AZ and sudo locale-gen az_AZ.UT8 on ubuntu server
  2. Tried setlocale(LC_ALL, "az-AZ"); or setlocale(LC_TIME, "az-AZ"); and all variations of az-AZ, aze, az_AZ on php file. No success!

What am I doing wrong?

like image 811
demonoid Avatar asked Dec 07 '25 11:12

demonoid


2 Answers

You may use php_intl extension if it is on.

if (version_compare(PHP_VERSION, '5.3.0', '<')) {
    exit ('IntlDateFormatter is available on PHP 5.3.0 or later.');
}    
if (!class_exists('IntlDateFormatter')) {
    exit ('You need to install php_intl extension.');
}

$fullDateFormatter = new IntlDateFormatter(
    'az_AZ',
    IntlDateFormatter::FULL,
    IntlDateFormatter::NONE
);
$longDateFormatter = new IntlDateFormatter(
    'az_AZ',
    IntlDateFormatter::LONG,
    IntlDateFormatter::NONE
);
$shortDateFormatter = new IntlDateFormatter(
    'az_AZ',
    IntlDateFormatter::SHORT,
    IntlDateFormatter::NONE
);
$customDateFormatter = new IntlDateFormatter(
    'az_AZ',
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    date_default_timezone_get(),
    IntlDateFormatter::GREGORIAN,
    'dd MMMM yyyy'
);

$datetime = new DateTime("2014-03-15 11:22:33");
echo $fullDateFormatter->format($datetime) . "\n";
echo $longDateFormatter->format($datetime) . "\n";
echo $shortDateFormatter->format($datetime) . "\n";
echo $customDateFormatter->format($datetime) . "\n";

The results are,

şənbə, 15, Mart, 2014
15 Mart , 2014
2014-03-15
15 Mart 2014
like image 143
akky Avatar answered Dec 09 '25 01:12

akky


Use setlocale(LC_TIME, 'your locale').

like image 20
Tural Ali Avatar answered Dec 09 '25 00:12

Tural Ali