Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

PHP 5 Recipes A Problem-Solution Approach 2005 phần 4 pot
MIỄN PHÍ
Số trang
50
Kích thước
487.2 KB
Định dạng
PDF
Lượt xem
1735

PHP 5 Recipes A Problem-Solution Approach 2005 phần 4 pot

Nội dung xem thử

Mô tả chi tiết

You might consider writing your own functions to deal with this type of situation, but this

is not very appealing, not only because it is extra work for a German-language site but because

the same task would then have to be repeated for each language. Fortunately, a much better

way to accomplish this task exists. You can use the setlocale() function to change PHP’s lan￾guage and related settings in a number of ways. Here you are concerned primarily with how

dates and times are represented, but if internationalization is of any concern to you in your

work with PHP, you should investigate this function more thoroughly.

setlocale() takes two arguments: a category (a predefined constant) and a language code (a

string). To localize time and date settings, you can use either LC_ALL or LC_TIME for the category.

You can determine whether the locale was set successfully by testing the return value of

setlocale(), which is either the language string on success or FALSE on failure. The languages

or locales actually supported will vary from system to system; if you cannot find a value for a

desired language or locale, check with your system administrator or consult the operating sys￾tem documentation. On Unix systems, you can often find out what is supported by examining

the contents of the /usr/share/locale directory. On Windows, use Regional Settings in the

Control Panel.

If you do not know ahead of time what is supported, you can pass multiple

language/locale strings to setlocale(), and PHP will test each one in succession until (you

hope) one is used successfully. For example:

<?php

if($lc = setlocale(LC_ALL, "de_DE", "de_DE@euro", "deu", "deu_deu", "german"))

echo "<p>Locale setting is \"$lc\".</p>";

else

echo "<p>Couldn't change to a German locale.</p>";

?>

Once you have set the locale, you are ready to output dates and times in the target lan￾guage without resorting to brute-force translation (and possible transliteration). However,

you cannot use date() for this. Instead, you must use a separate function, strftime(). This

function is similar to date() in that it takes a format string and an optional timestamp as

arguments. Unfortunately, the similarity ends there, because the formatting characters are

quite unlike those used by date(). Table 5-3 lists the characters you are most likely to need,

arranged by the part of the date or time they represent. Note that not all of these are available

on all platforms, and Windows has some of its own. See http://msdn.microsoft.com/

library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp for a complete listing.

Table 5-3. Format Characters Used by the strftime() Function

Character Description

Day

%A Full weekday name.

%a Abbreviated weekday name.

%u Weekday number (1 = Monday, 7 = Saturday).

%d Day of the month, with leading zero.

220 5-9 ■ DISPLAYING TIMES AND DATES IN OTHER LANGUAGES

5092_Ch05_FINAL 8/26/05 9:51 AM Page 220

Character Description

%e Day of the month, with leading space.

%j Day of the year (001–366). Note that numbering begins with 1 and not 0.

Week

%U Number of the week of the year, with Sunday as the first day of the week.

%V ISO-8601 number of the week of the year, with Monday as the first day of the

week (01–53).

%W Number of the week of the year, with Monday as the first day of the week

(decimal number).

Month

%B Full name of the month.

%b or %h Abbreviated name of the month.

%m Number of the month, with leading zero.

Year

%g Two-digit year for ISO-8601 week of the year.

%G Four-digit year for ISO-8601 week of the year.

%y Two-digit year.

%Y Four-digit year.

Hour

%H Hour (00–23).

%I Hour (01–12)

Minute

%M Minute.

Second

%S Second.

Full Date and/or Time

%c Preferred date and time representation for the current locale.

%D Current date; equivalent to %m/%d/%y.

%p a.m./p.m. indicator.

%R Time in 24-hour notation.

%r Time in 12-hour (am/pm) notation.

%T Current time; equivalent to %H:%M:%S.

%x Preferred date representation.

%X Preferred time representation.

%z or %Z Time zone.

Formatting Characters

%n New line.

%t Tab.

%% The percent character.

5-9 ■ DISPLAYING TIMES AND DATES IN OTHER LANGUAGES 221

5092_Ch05_FINAL 8/26/05 9:51 AM Page 221

Now you are ready to put this together in a simple working example. Actually, since

browsers have problems displaying more than one character set in a single page, we will

use three examples.

The Code

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

?>

<?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 '&#x0414&#x043E&#x0431&#x0440&#x043E&#x0435 '

. '&#x0423&#x0442&#x0440&#x043E! '

. '&#x0421&#x0435&#x0433&#x043E&#x0434&#x043D&#x044F '

. strftime('%A %d %B %Y', mktime()) . ".</p>\n";

}

else

echo "<p>Couldn't set a Russian locale.</p>\n";

?>

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

{

echo "<p>Sorry! No Chinese locale available on this system.</p>\n";

$lc_en = setlocale(LC_TIME, 'en_US', 'english');

echo "<p>Reverting locale to $lc_en.</p>\n";

}

?>

222 5-9 ■ DISPLAYING TIMES AND DATES IN OTHER LANGUAGES

5092_Ch05_FINAL 8/26/05 9:51 AM Page 222

How It Works

Figure 5-4 shows the output in a web browser from each of these scripts when run on a

Windows system that supports German and Russian locales but no Chinese locale.

Figure 5-4. Output from the three setlocale()/strftime() examples

■Note LC_TIME changes only the way in which dates and times are reported but does not change other

locale-dependent items such as character sets. If you use an English-language locale and need to display

dates in a language (German or Spanish, for example) that uses the Latin-1 character set or a close relative

such as ISO-8559-1, ISO-8859-15, or Windows-1252, you may be able to use LC_TIME. However, in the

case of a language that uses non-Latin characters (such as Russian, Chinese, and some Eastern European

languages with special characters not represented in Western European character sets), you will most likely

have to use LC_ALL. Be aware that using setlocale() with LC_ALL will change all locale settings, not just

those related to dates and times. If you will be working with currency, numbers, or sorting of strings, be sure

to check the PHP manual for setlocale() and understand what all the implications might be before doing so.

The format of the language string differs between Unix and Windows systems. On Unix

systems, this varies somewhat but generally takes the form lc_CC.charset, where lc repre￾sents the two-letter language code, CC represents the two-letter country code, and charset

is the designation of the character set to be used. (The charset designation—including the

period—is often optional.) For example, pt_BR.ISO-18859-1 might be used to represent Brazil￾ian Portuguese. On Windows, you can use either Microsoft's three-letter language codes or the

names of the languages, for example, deu or german for German-language dates and times.

5-9 ■ DISPLAYING TIMES AND DATES IN OTHER LANGUAGES 223

5092_Ch05_FINAL 8/26/05 9:51 AM Page 223

5-10. Generating Localized GMT/UTC Time and Date Strings

It is important to remember that using setlocale() to set LC_ALL or LC_TIME does not handle

time zone differences for you, as this example illustrates:

<?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 'Guten Abend! 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";

?>

The following shows the output for the date and time in standard format, along with the

name of the time zone, and then it shows a greeting, date, and time in German. As you can see

here, the time may be in German, but it is not German time that is being reported:

Fri, 18 Mar 2005 17:14:30 +1000 (E. Australia Standard Time)

Guten Abend! Heute ist Freitag 18 März 2005, 17.14 Uhr.

To report local time for any Germans who might be viewing this page, you have to calcu￾late the time zone offset yourself:

<?php

$ts_au = mktime(); // local time in Brisbane (GMT +1000)

$ts_de = $ts_au - (9 * 3600); // Berlin time is GMT +0100; difference is 9 hours

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) . '.';

?>

The output from the previous code snippet should look something this:

Good evening from Brisbane, where it's 18:03 on Friday 18 03 2005.

Guten Morgen aus Berlin. Hier ist es 09.30 Uhr, am Freitag dem 18 März 2005.

224 5-10 ■ GENERATING LOCALIZED GMT/UTC TIME AND DATE STRINGS

5092_Ch05_FINAL 8/26/05 9:51 AM Page 224

Tải ngay đi em, còn do dự, trời tối mất!