PHP/Utility Function/setlocale

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

Displaying Times and Dates in Other Languages

   <source lang="html4strict">

<?php

 $ts = mktime();
echo "

" . date("r (T)", $ts) . "

\n";
 if($loc_de = setlocale(LC_ALL, "de_DE@euro", "de_DE", "deu_deu"))
 {
echo "

Preferred locale for German on this system is \"$loc_de\".
"; echo "Guten Morgen! Heute ist " . strftime("%A %d %B %Y, %H.%M Uhr", $ts) . ".

\n";
 }
 else
echo "

Sorry! This system doesn"t speak German.

\n";

?>

 </source>
   
  


Generating Localized GMT/UTC Time and Date Strings

   <source lang="html4strict">

<?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) 
       . ".
"; 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) . ".";

?>

 </source>
   
  


Preferred locale for Chinese

   <source lang="html4strict">

<?php

   if($loc_zh = setlocale(LC_ALL, "zh_ZH.big5", "zh_ZH", "chn", "chinese")) { 
echo "

Preferred locale for Chinese on this system is \"$loc_zh\".
\n"; echo strftime("%A %d %B %Y", mktime()) . "

\n";
   } else { 
       $lc_en = setlocale(LC_TIME, "en_US", "english"); 
echo "

Reverting locale to $lc_en.

\n";
   } 

?>

 </source>
   
  


Preferred locale for German

   <source lang="html4strict">

<?php if($loc_de = setlocale(LC_ALL, "de_DE@euro", "de_DE", "deu_deu")) {

echo "

Preferred locale for German on this system is \"$loc_de\".
"; echo "Guten Morgen! Heute ist " . strftime("%A %d %B %Y", mktime()) . ".

\n";

} else

echo "

Sorry! This system doesn"t speak German.

\n";

?>

 </source>
   
  


Preferred locale for Russian

   <source lang="html4strict">

<?php

   if($loc_ru = setlocale(LC_ALL, "ru_RU.utf8", "rus_RUS.1251", "rus", "russian")) { 
echo "

Preferred locale for Russian on this system is \"$loc_ru\".
\n"; echo strftime("%A %d %B %Y", mktime()) . ".

\n";
   }else 
echo "

Couldn"t set a Russian locale.

\n";

?>

 </source>
   
  


setlocale(LC_MONETARY, "en_DK");

   <source lang="html4strict">

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

 </source>
   
  


setlocale(LC_MONETARY, "en_US");

   <source lang="html4strict">

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

 </source>
   
  


Setlocale() Options

   <source lang="html4strict">

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

 </source>
   
  


Setting a default locale

   <source lang="html4strict">

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

 </source>
   
  


Setting a default locale based on system environment

   <source lang="html4strict">

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

 </source>
   
  


To report local time for any Germans

   <source lang="html4strict">

<?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); ?>

 </source>