PHP/Utility Function/setlocale

Материал из Web эксперт
Перейти к: навигация, поиск

Displaying Times and Dates in Other Languages

 
<?php
  $ts = mktime();
  echo "<p>" . date("r (T)", $ts) . "</p>\n";
  if($loc_de = setlocale(LC_ALL, "de_DE@euro", "de_DE", "deu_deu"))
  {
    echo "<p>Preferred locale for German on this system is \"$loc_de\".<br />";
    echo "Guten Morgen! Heute ist " . strftime("%A %d %B %Y, %H.%M Uhr", $ts) 
    . ".</p>\n";
  }
  else
    echo "<p>Sorry! This system doesn"t speak German.</p>\n";
?>



Generating Localized GMT/UTC Time and Date Strings

 
<?php
  $ts_au = mktime();
  $ts_de = $ts_au - (9 * 3600);
  echo "Good evening from Brisbane, where it\"s " . date("H:m \o\n l d m Y", $ts_au) 
        . ".<br />";
  setlocale(LC_ALL, "de_DE", "german");
  echo "Guten Morgen aus Berlin. Hier ist es " 
        . strftime("%H:%M Uhr, am %A dem %d %B %Y", $ts_de) . ".";
?>



Preferred locale for Chinese

 
<?php 
    if($loc_zh = setlocale(LC_ALL, "zh_ZH.big5", "zh_ZH", "chn", "chinese")) { 
        echo "<p>Preferred locale for Chinese on this system is \"$loc_zh\".<br />\n"; 
        echo strftime("%A %d %B %Y", mktime()) . "</p>\n"; 
    } else { 
        $lc_en = setlocale(LC_TIME, "en_US", "english"); 
        echo "<p>Reverting locale to $lc_en.</p>\n"; 
    } 
?>



Preferred locale for German

 
<?php 
if($loc_de = setlocale(LC_ALL, "de_DE@euro", "de_DE", "deu_deu")) { 
    echo "<p>Preferred locale for German on this system is \"$loc_de\".<br />"; 
    echo "Guten Morgen! Heute ist " . strftime("%A %d %B %Y", mktime()) . ".</p>\n"; 
} else 
    echo "<p>Sorry! This system doesn"t speak German.</p>\n"; 
?>



Preferred locale for Russian

 
<?php 
    if($loc_ru = setlocale(LC_ALL, "ru_RU.utf8", "rus_RUS.1251", "rus", "russian")) { 
        echo "<p>Preferred locale for Russian on this system is \"$loc_ru\".<br />\n"; 
        echo  strftime("%A %d %B %Y", mktime()) . ".</p>\n"; 
    }else 
        echo "<p>Couldn"t set a Russian locale.</p>\n"; 
?>



setlocale(LC_MONETARY, "en_DK");

 
<?
$number = 1234.56; 
setlocale(LC_MONETARY, "en_DK"); 
?>



setlocale(LC_MONETARY, "en_US");

 
<?
$number = -1234.5672; 
setlocale(LC_MONETARY, "en_US"); 
?>



Setlocale() Options

 
OPTION             Value
 
LC_ALL             Modify all settings
 
LC_COLLATE         String comparison only
 
LC_TYPE            String classification (for example, differentiation between upper/lowercase)
 
LC_MONETARY        Currency values
 
LC_NUMERIC         Numeric values
 
LC_TIME            Date/time values



Setting a default locale

 
<?php
setlocale(LC_ALL,"es_US");
?>



Setting a default locale based on system environment

 
<?php
setlocale(LC_ALL,null);
?>



To report local time for any Germans

 
<?php 
$ts_au = mktime(); 
$ts_de = $ts_au - (9 * 3600);
echo date("H:m \o\n l d m Y", $ts_au); 
setlocale(LC_ALL, "de_DE", "german"); 
echo strftime("%H.%M Uhr, am %A dem %d %B %Y", $ts_de); 
?>