11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 23 • BUILDING WEB SITES FOR THE WORLDLocalizing Dates, Numbers, and TimesThe setlocale() function introduced in the previous section can go far beyond facilitating thelocalization of language; it can also affect how <strong>PHP</strong> renders dates, numbers, and times. This is importantbecause of the variety of ways in which this often crucial data is represented among different countries.For example, suppose you are a United States–based organization providing an essential subscriptionbasedservice to a variety of international corporations. When it is time to renew subscriptions, a specialmessage is displayed at the top of the browser that looks like this:Your subscription ends on 3-4-2011. Renew soon to avoid service cancellation.For the United States–based users, this date means March 4, 2011. However, for European users,this date is interpreted as April 3, 2011. The result could be that the European users won’t feel compelledto renew the service until the end of March, and therefore will be quite surprised when they attempt tolog in on March 5. This is just one of the many issues that might arise due to confusion over datarepresentation.You can eliminate such inconsistencies by localizing the information so that it appears exactly asthe user expects. <strong>PHP</strong> makes this a fairly easy task, done by setting the locale using setlocale(), andthen using functions such as money_format(), number_format(), and strftime() per usual to output thedata.For example, suppose you want to render the renewal deadline date according to the user’s locale.Just set the locale using setlocale(), and run the date through strftime() (also taking advantage ofstrtotime() to create the appropriate timestamp) like this:This produces the following:Your subscription ends on 04/03/2011The same process applies to formatting number and mo<strong>net</strong>ary values. For instance, the UnitedStates uses a comma as the thousands separator; Europe uses a period, a space, or nothing at all for thesame purpose. Making matters more confusing, the United States uses a period for the decimalseparator and Europe uses a comma for this purpose. As a result, the following numbers are ultimatelyconsidered identical:• 523,332.98• 523 332.98• 523332.98• 523.332,98Of course, it makes sense to render such information in a manner most familiar to the user in orderto reduce any possibility of confusion. To do so, you can use setlocale() in conjunction with455

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!