Hướng dẫn get_post_time wordpress - get_post_time

get_post_time (chuỗi & nbsp; $ format & nbsp; = & nbsp; 'u', bool & nbsp; $ gmt & nbsp; = & nbsp; false, int | wp_post & nbsp; $ post & nbsp; int | saistring $format = 'U', bool $gmt = false, int|WP_Post $post = null, bool $translate = false ): string|int|false

Lấy thời gian mà bài viết được viết.


$format StringOptionalstring Optional

Định dạng để sử dụng để truy xuất thời gian bài viết được viết. Chấp nhận định dạng ngày 'G', 'U' hoặc PHP. Mặc định 'U'.

Mặc định: 'U'

$gmt Booloptionalbool Optional

Có lấy thời gian GMT không.

Mặc định: false

$post int | wp_postoptionalint|WP_Post Optional

Đăng ID hoặc đăng đối tượng. Mặc định là đối tượng toàn cầu $post.

Mặc định:

return apply_filters( 'get_post_time', $time, $format, $gmt );
1

return apply_filters( 'get_post_time', $time, $format, $gmt );
2 Booloptionalbool Optional

Có nên dịch chuỗi thời gian không.

Mặc định: false


$post int | wp_postoptional Formatted date string or Unix timestamp if $format is 'U' or 'G'.
False on failure.


Đăng ID hoặc đăng đối tượng. Mặc định là đối tượng toàn cầu $post.

function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$source   = ( $gmt ) ? 'gmt' : 'local';
	$datetime = get_post_datetime( $post, 'date', $source );

	if ( false === $datetime ) {
		return false;
	}

	if ( 'U' === $format || 'G' === $format ) {
		$time = $datetime->getTimestamp();

		// Returns a sum of timestamp with timezone offset. Ideally should never be used.
		if ( ! $gmt ) {
			$time += $datetime->getOffset();
		}
	} elseif ( $translate ) {
		$time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
	} else {
		if ( $gmt ) {
			$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
		}

		$time = $datetime->format( $format );
	}

	/**
	 * Filters the localized time a post was written.
	 *
	 * @since 2.6.0
	 *
	 * @param string|int $time   Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
	 * @param string     $format Format to use for retrieving the time the post was written.
	 *                           Accepts 'G', 'U', or PHP date format.
	 * @param bool       $gmt    Whether to retrieve the GMT time.
	 */
	return apply_filters( 'get_post_time', $time, $format, $gmt );
}

Mặc định:

return apply_filters( 'get_post_time', $time, $format, $gmt );
1 View on GitHub


return apply_filters( 'get_post_time', $time, $format, $gmt );
2 Booloptional
( 'get_post_time', string|int $time, string $format, bool $gmt )

Có nên dịch chuỗi thời gian không.



Chuỗi | int | Sai Định dạng chuỗi ngày hoặc dấu thời gian UNIX nếu $format'U' hoặc 'G'. Sai về thất bại.
Tệp:
return apply_filters( 'get_post_time', $time, $format, $gmt );
7. Xem tất cả các tài liệu tham khảo
Xem trên Tracview trên GitHub
2.0.0 Ứng dụng_filters ('get_post_time', String | int $ Time, String $ format, bool $ gmt)

Y/m/d g:i:s A – 2022/11/06 12:50:48 AM( 'get_post_time', string|int $time, string $format, bool $gmt )

Y/m/d – 2022/11/06


apply_filters( 'get_post_time', string|int$time, string$format, bool$gmt )string|int

Filters the localized time a post was written.

return apply_filters( 'get_post_time', $time, $format, $gmt );
8 string|intstring

Formatted date string or Unix timestamp if $format is 'U' or 'G'.
Accepts 'G', 'U', or PHP date format.

$format stringbool

Format to use for retrieving the time the post was written. Accepts 'G', 'U', or PHP date format.


$gmt bool

return apply_filters( 'get_post_time', $time, $format, $gmt );

Whether to retrieve the GMT time. View on GitHub



File:
return apply_filters( 'get_post_time', $time, $format, $gmt );
7. View all references
View on TracView on GitHub Changelog
2.6.0 Version

get_the_date là hàm trả về thời gian bài viết được publish theo dạng string. Tức là khi dùng hàm get_the_date() để lấy dữ liệu, muốn hiển thị nó ra thì ta phải echo dữ liệu đó. Định dạng ngày tháng được hỗ trợ theo đúng chuẩn PHP Date Format. là hàm trả về thời gian bài viết được publish theo dạng string. Tức là khi dùng hàm get_the_date() để lấy dữ liệu, muốn hiển thị nó ra thì ta phải echo dữ liệu đó. Định dạng ngày tháng được hỗ trợ theo đúng chuẩn PHP Date Format.

Table of Contents

  • 1 Giới thiệu về hàm get_the_date Giới thiệu về hàm get_the_date
  • 2 Cách sử dụng hàm get_the_date Cách sử dụng hàm get_the_date
    • 2.1 Một số ví dụ về cách dùng get_the_date() Một số ví dụ về cách dùng get_the_date()
    • 2.2 Đồng nhất định dạng ngày tháng năm trên toàn bộ website Đồng nhất định dạng ngày tháng năm trên toàn bộ website
    • 2.3 Một số ví dụ về định dạng ngày tháng năm Một số ví dụ về định dạng ngày tháng năm

Giới thiệu về hàm get_the_date

Đây là hàm nằm trong lõi (core) của WordPress. Bạn có thể tìm thấy hàm get_the_date ở file này

return apply_filters( 'get_post_time', $time, $format, $gmt );
7

functionget_the_date($format='',$post=null){get_the_date($format='',$post=null){

    $post=get_post($post);$post=get_post($post);

    if(!$post){if(!$post){

        returnfalse;returnfalse;

    }}

    if(''===$format){if( ''===$format){

        $the_date=get_post_time(get_option('date_format'),false,$post,true);$the_date=get_post_time(get_option('date_format'),false, $post,true);

    }else{}else{

        $the_date=get_post_time($format,false,$post,true);$the_date=get_post_time($format,false, $post,true);

    }}

    returnapply_filters('get_the_date',$the_date,$format,$post);returnapply_filters('get_the_date',$the_date,$format,$post);

}

get_the_date($format,$post_id);($format,$post_id);

Trong đó $format là định dạng thời gian. Định dạng này hỗ trợ theo chuẩn PHP DateTime Format$format là định dạng thời gian. Định dạng này hỗ trợ theo chuẩn PHP DateTime Format

$post_id là ID của bài viết / page cần lấy thông tin thời gian. là ID của bài viết / page cần lấy thông tin thời gian.

Hướng dẫn get_post_time wordpress - get_post_time

Hướng dẫn sử dụng hàm get_the_date() trong WordPress

Một số ví dụ về cách dùng get_the_date()

Lấy thông tin thời gian trong vòng lặp

Một số ví dụ về cách dùng get_the_date()time datetime="echoget_the_date('c');?>"itemprop="datePublished">echoget_the_date(); ?></time>

Muốn lấy thời gian của bài viết / page hiện tại đang xem:

echoget_the_date(get_the_ID());get_the_date(get_the_ID());

Lấy thời gian publish của bài viết hiện tại đang xem và đổi định dạng ngày tháng năm thành Monday January 1st, 2022:

echoget_the_date('l F dS, Y',get_the_ID());get_the_date('l F dS, Y',get_the_ID());

Thay đổi định dạng ngày tháng năm hiển thị ra ngoài:

Ví dụ muốn hiển thị: Monday January 11, 2022

$post_date=get_the_date('l F j, Y');echo$post_date;=get_the_date('l F j, Y');echo$post_date;

Ví dụ muốn hiển thị: Wed Jan 9, 2022

$post_date=get_the_date('D M j, Y');echo$post_date;=get_the_date('D M j, Y');echo$post_date;

Hiển thị dạng 15th Jan 2022

echoget_the_date('dS M Y',$post->ID);get_the_date('dS M Y',$post->ID);

Lấy thời gian publish của bài viết bất kỳ với id là $id:

Cộng thêm 1 ngày vào thời gian publish bài viết. Ví dụ bạn publish bài viết vào ngày 10 October nhưng bạn muốn nó hiện ra vào ngày 11 October:

$date_add_1_day=date('j-F-Y',strtotime('+1 day',get_the_date('U')));=date('j-F-Y',strtotime('+1 day',get_the_date('U')));

echo"Published on: $date_add_1_day\n";"Published on: $date_add_1_day\n";

Thay +1 day thành số ngày bạn muốn thêm.

So sánh thời gian giữa lúc bài viết được published với thời gian hiện tại. Ví dụ hiện ra kiểu: Bài viết được public 1 năm, 2 tháng, 3 ngày trước. Hoặc bài viết được published 330 ngày trước:

$difference=get_the_date('U',$id)->diff(time());=get_the_date('U',$id)->diff(time());

echo'Difference: '.$difference->y.' years, ''
Difference:
'
.$difference->y.' years,
'

                   .$difference->m.' months, '.$difference->m.' months,
'

                   .$difference->d.' days';.$difference->d.' days

'
;

echo'Số ngày: '.  $difference->days;'Số ngày: '.  $difference->days;

Nói chung, muốn thay đổi định dạng ngày tháng năm các bạn chỉ cần đổi tham số thứ nhất theo đúng định dạng được hỗ trợ theo chuẩn PHP DateTime Format. Các ký tự định dạng được tiêu chuẩn hóa và sử dụng toàn cầu trong ngôn ngữ lập trình PHP. Vì WordPress được viết bằng ngôn ngữ lập trình PHP nên bạn có thể sử dụng bảng ký tự định dạng Ngày và Giờ trực tiếp từ trang web php.net.PHP DateTime Format. Các ký tự định dạng được tiêu chuẩn hóa và sử dụng toàn cầu trong ngôn ngữ lập trình PHP. Vì WordPress được viết bằng ngôn ngữ lập trình PHP nên bạn có thể sử dụng bảng ký tự định dạng Ngày và Giờ trực tiếp từ trang web php.net.

Đây là bảng các ký tự chuẩn hóa hay dùng được trích ra từ đó:

Day of Month
$format8 Numeric, with leading zeros 01–31
$format9 Numeric, without leading zeros 1–31
'G'0 The English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
Weekday
'G'1 Full name  (lowercase ‘L’) Sunday – Saturday
'G'2 Three letter name Mon – Sun
Month
'G'3Numeric, with leading zeros 01–12
'G'4 Numeric, without leading zeros 1–12
'G'5 Textual full January – December
'G'6 Textual three letters Jan – Dec
Year
'G'7 Numeric, 4 digits Eg., 1999, 2022
'G'8 Numeric, 2 digits Eg., 99, 03
Time
'G'9 Lowercase am, pm
'U'0 Uppercase AM, PM
'U'1 Hour, 12-hour, without leading zeros 1–12
'G'5 Textual full 01–12
January – December 'G'6 Textual three letters
Jan – Dec Year'G'7
Numeric, 4 digits Eg., 1999, 2022 'G'8
Numeric, 2 digits Eg., 99, 03 'G'8
Numeric, 2 digits Eg., 99, 03 Time
'G'9
Lowercase am, pm 'U'0
Uppercase AM, PM 'U'1
Hour, 12-hour, without leading zeros 'U'2 1455880176

Hour, 12-hour, with leading zeros

'U'3
1, Cấu hình định dạng ngày tháng cho toàn website trong phần Setting/General của WordPress Dashboard.

Hour, 24-hour, without leading zeros

get_the_date(get_option('date_format'));(get_option('date_format'));

0-23

'U'4

  • Hour, 24-hour, with leading zeros
  • 00-23
  • 'U'5
  • Minutes, with leading zeros
  • 00-59
  • 'U'6
  • Seconds, with leading zeros
  • 'U'7
  • Timezone abbreviation
  • Eg., EST, MDT …
  • Full Date/Time
References: