Hướng dẫn format date php dd/mm/yyyy - định dạng ngày php dd / mm / yyyy

Tôi có thể thấy câu trả lời tuyệt vời, vì vậy không cần phải lặp lại ở đây, vì vậy tôi muốn đưa ra một số lời khuyên:

Tôi khuyên bạn nên sử dụng số nguyên thời gian UNIX thay vì định dạng ngày có thể đọc được để xử lý thời gian trong nội bộ, sau đó sử dụng hàm date() của PHP để chuyển đổi giá trị dấu thời gian thành định dạng ngày có thể đọc được của người để hiển thị người dùng. Đây là một ví dụ thô thiển về cách nó nên được thực hiện:

// Get unix timestamp in seconds
$current_time = date();

// Or if you need millisecond precision

// Get unix timestamp in milliseconds
$current_time = microtime(true);

Sau đó, sử dụng $current_time khi cần thiết trong ứng dụng của bạn (lưu trữ, thêm hoặc trừ, v.v.), sau đó khi bạn cần hiển thị giá trị ngày cho người dùng của mình, bạn có thể sử dụng date() để chỉ định định dạng ngày mong muốn của mình:

// Display a human-readable date format
echo date('d-m-Y', $current_time);

Bằng cách này, bạn sẽ tránh được nhiều vấn đề đau đầu với các định dạng ngày, chuyển đổi và múi giờ, vì ngày của bạn sẽ ở định dạng tiêu chuẩn hóa (thời gian unix) nhỏ gọn, độc lập với thời gian (luôn luôn ở UTC) và được hỗ trợ rộng rãi trong các ngôn ngữ và cơ sở dữ liệu lập trình .

Ngày trong các định dạng

// Display a human-readable date format
echo date('d-m-Y', $current_time);
1hoặc
// Display a human-readable date format
echo date('d-m-Y', $current_time);
2định dạng được định nghĩa bằng cách nhìn vào dấu phân cách giữa các thành phần khác nhau: nếu dấu phân cách là dấu gạch chéo (
// Display a human-readable date format
echo date('d-m-Y', $current_time);
3), thì người Mỹ
// Display a human-readable date format
echo date('d-m-Y', $current_time);
1được giả sử; trong khi nếu dấu phân cách là dấu gạch ngang (
// Display a human-readable date format
echo date('d-m-Y', $current_time);
5) hoặc dấu chấm (
// Display a human-readable date format
echo date('d-m-Y', $current_time);
6), thì
// Display a human-readable date format
echo date('d-m-Y', $current_time);
2 định dạng châu Âu được giả sử. Kiểm tra thêm ở đây .định dạng
// Display a human-readable date format
echo date('d-m-Y', $current_time);
1hoặc
// Display a human-readable date format
echo date('d-m-Y', $current_time);
2định dạng được định nghĩa bằng cách nhìn vào dấu phân cách giữa các thành phần khác nhau: nếu dấu phân cách là dấu gạch chéo (
// Display a human-readable date format
echo date('d-m-Y', $current_time);
3), thì người Mỹ
// Display a human-readable date format
echo date('d-m-Y', $current_time);
1được giả sử; trong khi nếu dấu phân cách là dấu gạch ngang (
// Display a human-readable date format
echo date('d-m-Y', $current_time);
5) hoặc dấu chấm (
// Display a human-readable date format
echo date('d-m-Y', $current_time);
6), thì
// Display a human-readable date format
echo date('d-m-Y', $current_time);
2 định dạng châu Âu được giả sử. Kiểm tra thêm ở đây .

Sử dụng chức năng ngày mặc định.

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );

EDIT Tôi vừa thử nghiệm nó, và bằng cách nào đó, PHP không hoạt động tốt với định dạng dd / mm / yyyy. Đây là một giải pháp khác. Tôi vừa thử nghiệm nó, và bằng cách nào đó, PHP không hoạt động tốt với định dạng dd / mm / yyyy. Đây là một giải pháp khác.

$var = '20/04/2012';
$date = str_replace('/', '-', $var);
echo date('Y-m-d', strtotime($date));

209 hữu ích 5 bình luận chia sẻ 5 bình luận chia sẻ

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:53Aug 2, 2011 at 10:53

Use

// Display a human-readable date format
echo date('d-m-Y', $current_time);
8 and
// Display a human-readable date format
echo date('d-m-Y', $current_time);
9 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:03Aug 2, 2011 at 11:03

5

I found that

// Display a human-readable date format
echo date('d-m-Y', $current_time);
8 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
$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
1 php extension.

  • Install

    $var = "20/04/2012";
    echo date("Y-m-d", strtotime($var) );
    1 if necesarry (ubuntu):
    $var = "20/04/2012";
    echo date("Y-m-d", strtotime($var) );
    3

  • Install the locale you want to use (I'm using italian as an example):

    $var = "20/04/2012";
    echo date("Y-m-d", strtotime($var) );
    4

  • Generate a locally formatted date:

$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()); 
// Output: 6 gennaio 2016 12:10

answered Jun 7, 2016 at 11:55Jun 7, 2016 at 11:55

Simon EpskampSimon EpskampSimon Epskamp

8,0463 gold badges52 silver badges56 bronze badges3 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

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
61 class (quoting) :
$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
6
1 class (quoting) :

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

There are a couple of examples on the manual page of

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
7, 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:56Aug 2, 2011 at 10:56

Pascal MARTINPascal MARTINPascal MARTIN

388k77 gold badges646 silver badges656 bronze badges77 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 International Components for Unicode (ICU) Data

answered Nov 4, 2020 at 13:38Nov 4, 2020 at 13:38

DhruvDhruvDhruv

1,7341 gold badge5 silver badges6 bronze badges1 gold badge5 silver badges6 bronze badges

If you are trying to convert a

$var = "20/04/2012";
echo date("Y-m-d", strtotime($var) );
8 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 badges24 gold badges76 silver badges98 bronze badges

answered Jan 7, 2014 at 2:48Jan 7, 2014 at 2:48