Localize current time in PHP
Asked Answered
A

3

5

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.

Ardenia answered 13/9, 2009 at 22:26 Comment(0)
G
9

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.

Goodfellowship answered 13/9, 2009 at 22:30 Comment(3)
The simple solution I was looking for. Danke.Ardenia
Doesn't this assume that you have to be using the zend framework? which isn't mentioned in the question.Rheumy
@WebDevHobo - no, you don't have to use the whole thing, its componenents are loosely coupled by design.Goodfellowship
H
5

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

Hewitt answered 13/9, 2009 at 22:29 Comment(1)
It may be worth noting that IntlDateFormatter::format($time) will fail silently if $time is a string. Make sure that you always pass an integer! (Tested with Ubuntu 10.04 LTS and PHP 5.3.)Annatto
W
2

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.

Was answered 27/9, 2010 at 19:15 Comment(1)
IMHO, both IntlDateFormatter (provided that you're running PHP 5.3 with intl installed) and Zend_Date are simpler than anything that requires setlocale().Plural

© 2022 - 2024 — McMap. All rights reserved.