Hướng dẫn php date locale

I have this MySQL query, which returns two dates [which are both formatted as a-m-Y]. Now I want to translate this date into my own language [Danish]. How can I do that. I have tried both the setlocale[] and strftime[] functions, but it won't work. I know it's a very basic question, but i really need help :] Thanks a lot!

asked Aug 2, 2011 at 10:53

Use setlocale and strftime together:

setlocale[LC_TIME, array['da_DA.UTF-8','[email protected]','da_DA','danish']];
echo strftime["%A"]; // outputs 'tirsdag'

Works on my php installation on Windows.

strftime[]: Warning! This function has been DEPRECATED as of PHP 8.1.0. Relying on this function is highly discouraged.

answered Aug 2, 2011 at 11:03

5

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.

  • Install intl if necesarry [ubuntu]: sudo apt-get install php5-intl

  • Install the locale you want to use [I'm using italian as an example]: sudo locale-gen it_IT

  • Generate a locally formatted date:

$fmt = new \IntlDateFormatter['it_IT', NULL, NULL];
$fmt->setPattern['d MMMM yyyy HH:mm']; 
// See: //userguide.icu-project.org/formatparse/datetime for pattern syntax
echo $fmt->format[new \DateTime[]]; 
// Output: 6 gennaio 2016 12:10

answered Jun 7, 2016 at 11:55

Simon EpskampSimon Epskamp

8,0463 gold badges52 silver badges56 bronze badges

1

I don't think the date[] function is quite evolved enough for you, here.

Instead, I would recommend you take a look at the IntlDateFormatter1 class [quoting] :

Date Formatter is a concrete class that enables locale-dependent formatting/parsing of dates using pattern strings and/or canned patterns.

There are a couple of examples on the manual page of IntlDateFormatter::format[], where that method is used to display a date in two different languages, by just setting the desired locale.


1. bundled with PHP >= 5.3

answered Aug 2, 2011 at 10:56

Pascal MARTINPascal MARTIN

388k77 gold badges646 silver badges656 bronze badges

2

This Will Surely works for you if you want norwegian date and month format

$date = '2016-11-16 05:35:14';
setlocale[LC_TIME, array['nb_NO.UTF-8','[email protected]','nb_NO','norwegian']];
echo strftime["%e %b %Y",strtotime[$date]];

if you want to get other language locale ids like nb_NO then refer this site International Components for Unicode [ICU] Data

answered Nov 4, 2020 at 13:38

DhruvDhruv

1,7341 gold badge5 silver badges6 bronze badges

If you are trying to convert a datetime try this:

$fecha = $dateConsulta->format['d-M-Y'];  
$fecha = str_replace['Jan','Ene',$fecha];
$fecha = str_replace['Apr','Abr',$fecha];
$fecha = str_replace['Aug','Ago',$fecha];
$fecha = str_replace['Dec','Dic',$fecha];

Pranav Singh

15.1k24 gold badges76 silver badges98 bronze badges

answered Jan 7, 2014 at 2:48

Cảm ơn Rico Neitzel về gợi ý. Thay vì cố gắng định dạng ngày php, hãy sử dụng strftime. Để xem 3 chữ cái đầu tiên của tên tháng bằng ngôn ngữ của bạn [Ví dụ: Dez thay vì Dec từ Dezembro chứ không phải December], hãy làm theo hướng dẫn cài đặt ngôn ngữ ở trên, sau đó:

Nội dung chính

  • 1. Cách thiết lập.
  • 1.1 Sử dụng file php
  • 1.2 Sử dụng file json
  • 2. Website thay đổi ngôn ngữ theo người dùng
  • 2.1 Sử dụng session và middleware
  • 2.2 Sử dụng subdomain
  • 2.3 Sử dụng trên url

Nội dung chính

  • 1. Cách thiết lập.
  • 1.1 Sử dụng file php
  • 1.2 Sử dụng file json
  • 2. Website thay đổi ngôn ngữ theo người dùng
  • 2.1 Sử dụng session và middleware
  • 2.2 Sử dụng subdomain
  • 2.3 Sử dụng trên url

Nội dung chính

  • 1. Cách thiết lập.
  • 1.1 Sử dụng file php
  • 1.2 Sử dụng file json
  • 2. Website thay đổi ngôn ngữ theo người dùng
  • 2.1 Sử dụng session và middleware
  • 2.2 Sử dụng subdomain
  • 2.3 Sử dụng trên url

lệnh date: date ['d M Y'] // không thể thay đổi từ tiếng Anh

setlocale[ LC_ALL, "pt_BR"]; // Portuguese, replace with your locale
echo strftime['%e %b %G'];
result: "4 Dez 2016"

/**
 * datelo funcion [date with locale]
 * Credits: Sergio Abreu 
 * //sites.sitesbr.net
 * NOTE: Depend on availability of the locale in server.
 *
 */

function datelo[ $str, $locale='en_US', $time=null]{

  if[ $time === null]{  $time = time[]; }

  if [ preg_match["/[DlFM]/", $str]]{

     setlocale[LC_ALL, $locale];

     $dict = array[ 'd'=>'%d', 'D'=>'%a', 'j'=>'%e', 'l'=>'%A', 'N'=>'%u', 'w'=>'%w', 'F'=>'%B', 
      'm'=>'%m', 'M'=>'%b', 'Y'=>'%G', 'g'=>'%l', 'G'=>'%k', 'h'=>'%I', 'H'=>'%H', 'i'=>'%M', 
      's'=>'%S', 'S'=>'', 'z'=>'%j', 'n'=>'%m', ' '=>' ', '-'=>'-', '/'=>'/', ':'=>':', ','=>','];

     $chars = preg_split["//", $str];
     $nstr = '';

     foreach [$chars as $c]{
        if [$c]{ //skip empties
          $nc = $dict[$c];
          if[ $c === 'n']{ // Fixes the extra zero
            $nc = preg_replace["/^0+/", '', strftime[ $nc]];   
          }
          elseif[ $c === 'z']{ // Fixes the extra zero and decrease 1
            $nc = preg_replace["/^0+/", '',  strftime[ $nc]]; // 023 turns 23
            $nc = intval[$nc] - 1;
          }          
          $nstr .= $nc;
        }
   }
   return strftime[ $nstr];     

  }else{ // not localized
    return date[ $str, $time];
 } 
}

-1 hữu ích 0 bình luận chia sẻ

Các website ngày nay muốn tiếp cận với nhiều loại khách hàng thì đều cần phải sử dụng đa ngôn ngữ [i18n]. Với những ai sử dụng Laravel cho việc phát triển website thì vấn đề i18n được hỗ trợ và xử lý rất đơn giản. Bài viết này mình sẽ giới thiệu đến các bạn một số cách để xử lý i18n trong Laravel.

Nội dung chính

  • 1. Cách thiết lập.
  • 1.1 Sử dụng file php
  • 1.2 Sử dụng file json
  • 2. Website thay đổi ngôn ngữ theo người dùng
  • 2.1 Sử dụng session và middleware
  • 2.2 Sử dụng subdomain
  • 2.3 Sử dụng trên url

Nội dung chính

  • 1. Cách thiết lập.
  • 1.1 Sử dụng file php
  • 1.2 Sử dụng file json
  • 2. Website thay đổi ngôn ngữ theo người dùng
  • 2.1 Sử dụng session và middleware
  • 2.2 Sử dụng subdomain
  • 2.3 Sử dụng trên url

1. Cách thiết lập.

1.1 Sử dụng file php

  • Trong thư mục /resources/lang/ ta thêm các folder chứa các ngôn ngữ mà muốn chuyển đổi, như ví dụ dưới đây mình sẽ tạo 2 folder là en và vi để chứa ngôn ngữ của Tiếng Anh và Tiếng Việt.
/resources /lang /en messages.php /vi messages.php

Trong các folder ta tạo các file php và đặt tên sao cho phù hợp, như trên giả sử mình tạo file là message.php, trong cả 2 folder ban đều tạo các file giống nhau và nội dung trong các file mình tạo như sau. /resources/lang/en/message.php

Chủ Đề