Localize current time in PHP

前端 未结 3 1604
情歌与酒
情歌与酒 2021-01-13 01:04

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

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

As simple

3条回答
  •  星月不相逢
    2021-01-13 02:06

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

提交回复
热议问题