PHP Date function output in Italian

后端 未结 6 1187
误落风尘
误落风尘 2020-11-29 08:17

I am trying to output dates in the Italian format using date() as follows:



        
相关标签:
6条回答
  • 2020-11-29 08:53

    date() is not locale-aware. You should use strftime() and its format specifiers to output locale-aware dates (from the date() PHP manual):

    To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

    Regarding Anti Veeranna's comment: he is absolutely right, since you have to be very careful with setting locales as they are sometimes not limited to the current script scope. The best way would be:

    $oldLocale = setlocale(LC_TIME, 'it_IT');
    echo utf8_encode( strftime("%a %d %b %Y", $row['eventtime']) );
    setlocale(LC_TIME, $oldLocale);
    
    0 讨论(0)
  • 2020-11-29 08:54

    http://www.phpnews.it/articoli/ottenere-date-in-italiano/

    0 讨论(0)
  • 2020-11-29 08:54

    After a lot of attempts, this is the solution works like charm:

    STEP 1: We will set the localization and time zone (upper page)

    // ====> Localizzazione
    
        setlocale(LC_TIME, 'ita', 'it_IT');        // Italiano
        date_default_timezone_set("Europe/Rome");  // Ora
    

    STEP 2: We make a function for easily convert when we need (wherever into page)

    //=============== Funzione formattazione data
    function f_date_time($f_date, $f_date_info) {
    
        if (strlen($f_date) > 7){
            $dateToParse = $f_date;
        }else{
            $dateToParse = date("YmdHis");
        }
    
    
        switch ($f_date_info) {
            case 1: //20191130
                echo date("Ymd");
                break;
            case 2: //30/11/19
                echo utf8_encode(strftime('%d/%m/%y', strtotime($dateToParse)));
                break;
            case 3: //30/11/2019
                echo utf8_encode(strftime('%d/%m/%Y', strtotime($dateToParse)));
                break;
            case 4: //30/11/2019 15:27:50
                echo utf8_encode(strftime('%d/%m/%Y %H:%M:%S', strtotime($dateToParse)));
                break;
            case 5: //Saturday 30
                echo utf8_encode(strftime('%A %d', strtotime($dateToParse)));
                break;
            case 6: //Saturday 30 November
                echo utf8_encode(strftime('%A %d %B', strtotime($dateToParse)));
                break;
            case 7: //Saturday 30 November 2019
                echo utf8_encode(strftime('%A %d %B %Y', strtotime($dateToParse)));
                break;
            case 8: //Saturday 30 November 2019 ore 15:27:50
                echo utf8_encode(strftime('%A %d %B %Y %H:%M:%S', strtotime($dateToParse)));
                break;          
            default: //20191130152750
                echo date("YmdHis");;
        }
    
    }
    

    STEP 3: Now to convert your data just use the function (wherever into your code)

    $today = "20190320234500"; //Format yyyymmddhhmmss
    
    echo f_date_time($today, 1) . "\n";
    echo f_date_time($today, 2) . "\n";
    echo f_date_time($today, 3) . "\n";
    echo f_date_time($today, 4) . "\n";
    echo f_date_time($today, 5) . "\n";
    echo f_date_time($today, 6) . "\n";
    echo f_date_time($today, 7) . "\n";
    echo f_date_time($today, 8) . "\n";
    

    OUTPUT: will be somethink like this:

    20191231
    20/03/74
    20/03/1974
    20/03/1974 23:45:00
    mercoledì 20
    mercoledì 20 marzo
    mercoledì 20 marzo 1974
    mercoledì 20 marzo 1974 23:45:00
    

    PS: happy birday to me :-)

    0 讨论(0)
  • 2020-11-29 09:02

    About the article on http://www.phpnews.it/articoli/ottenere-date-in-italiano/response, the blog suggest an alternative method, but the code is not working, here is the correct code:

    function timestamp_to_date_italian($date)
        {       
            $months = array(
                    '01' => 'Gennaio', 
                    '02' => 'Febbraio', 
                    '03' => 'Marzo', 
                    '04' => 'Aprile',
                    '05' => 'Maggio', 
                    '06' => 'Giugno', 
                    '07' => 'Luglio', 
                    '08' => 'Agosto',
                    '09' => 'Settembre', 
                    '10' => 'Ottobre', 
                    '11' => 'Novembre',
                    '12' => 'Dicembre');
    
            list($day, $month, $year) = explode('-',date('d-m-Y', $date));      
            return $day . ' ' . $months[$month] . ' ' . $year;
    
        }
    
    0 讨论(0)
  • 2020-11-29 09:05

    it_IT locale has to be installed/enabled by your server admin, otherwise this will not work.

    So, Jonathan's solution is probably the best.

    0 讨论(0)
  • 2020-11-29 09:17

    I found that setlocale isn't reliable, as it is set per process, not per thread (the manual mentions this). This means other running scripts can change the locale at any time. A solution is using IntlDateFormatter from the intl php extension.

    $fmt = new \IntlDateFormatter('it_IT', NULL, NULL);
    $fmt->setPattern('d MMMM yyyy HH:mm'); 
    // See: http://userguide.icu-project.org/formatparse/datetime for pattern syntax
    echo $fmt->format(new \DateTime()); 
    

    If it doesn't work you might need to:

    • Install intl php extension (ubuntu example): sudo apt-get install php5-intl

    • Install the locale you want to use: sudo locale-gen it_IT

    0 讨论(0)
提交回复
热议问题