Localize current time in PHP

£可爱£侵袭症+ 提交于 2019-12-01 03:07:29

问题


Trying to display current time with PHP (using this):

$date = date('m/d/Y h:i:s a', time());                      
echo $date;

As simple as it gets. How do I localize it? I want to translate the months and days to Hebrew.

Thanks.


回答1:


Zend_Date is completely internationalized. You should check that out for a simple way to do it:

All full and abbreviated names of months and weekdays are supported for more than 130 languages. Methods support both input and the output of dates using the localized names of months and weekdays, in the conventional format associated with each locale.




回答2:


Actually, I don't think it is quite possible in PHP 5.2 :-(

At least, not with what's bundled with/in PHP (There are libraries coded in PHP that you could use, though, like other answers pointed out)


With PHP 5.3, though, you have the IntlDateFormatter class, which does exactly what you want :

This class represents the ICU date formatting functionality. It allows users to display dates in a localized format or to parse strings into PHP date values using pattern strings and/or canned patterns.

For instance, using that class, like this :

echo IntlDateFormatter::create('fr_FR', IntlDateFormatter::FULL, IntlDateFormatter::FULL)->format(time(time())) . "\n";
echo IntlDateFormatter::create('fr_FR', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT)->format(time(time())) . "\n";

echo IntlDateFormatter::create('zh-Hant-TW', IntlDateFormatter::FULL, IntlDateFormatter::FULL)->format(time(time())) . "\n";
echo IntlDateFormatter::create('zh-Hant-TW', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT)->format(time(time())) . "\n";

echo IntlDateFormatter::create('en_US', IntlDateFormatter::FULL, IntlDateFormatter::FULL)->format(time(time())) . "\n";
echo IntlDateFormatter::create('en_US', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT)->format(time(time())) . "\n";

You'd get :

dimanche 9 novembre 2008 23:54:47 GMT+00:00
9 nov. 2008 23:54
2008年11月9日星期日 下午11時54分47秒 GMT+00:00
2008/11/9 下午 11:54
Sunday, November 9, 2008 11:54:47 PM GMT+00:00
Nov 9, 2008 11:54 PM

Which looks quite nice, doesn't it ?

Sad thing is PHP 5.3 is only a few months old, and not available on many hosting services... And will require testing (and probably fixes) for your application...


Thinking about it : maybe you can install the PECL intl extension on PHP 5.2, though, and get the same functionnality...




回答3:


If you need a more simple way than Zend_Date and IntlDateFormatter try the standard strftime function in php. Here's the stuff I did to get it up and running with the Russian language running php 5.3 on Ubuntu (Russian locale was not installed).

To install locale

  1. cd /usr/share/locales
  2. sudo ./install-language_pack ru_RU
  3. sudo dpkg-reconfigure locales
  4. Restart Apache

Next, use the following php snippet:

setlocale(LC_TIME, 'ru_RU.UTF-8');
echo strftime('%A', time());

Should output today's day of the week.



来源:https://stackoverflow.com/questions/1419018/localize-current-time-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!