“Formatting Date and Time”的版本间的差异

来自WordPress中文文档
跳转至: 导航搜索
(1个修订)
(1个版本)
 
(未显示3个用户的3个中间版本)
第1行: 第1行:
 +
{{Languages|
 +
{{en|Formatting Date and Time}}
 +
{{fr|Modifier_Date_et_Heure}}
 +
{{ja|Formatting Date and Time}}
 +
{{pt-br|Formatando Data e Hora}}
 +
{{ru|Форматирование даты и времени}}
 +
{{zh-cn|自定义时间和日期格式}}
 +
{{zh-tw|自訂時間和日期}}
 +
{{it|Formattando_Data_e_Ora}}
 +
}}
 +
 
==Customizing the Time and Date==
 
==Customizing the Time and Date==
  
Certain WordPress tag functions are used to display or return date and time information; [[Template Tags/the date|the_date()]] and [[Template Tags/the time|the_time()]] are examples of this. Some of these functions accept a parameter called a format string that allows you to determine how the date is going to be displayed. The format string is a template in which various parts of the date are combined (using format characters) to generate a date in the format specified.
+
Certain WordPress tag functions are used to display or return date and time information; [[Template Tags/the date|the_date()]] and [[Template Tags/the time|the_time()]] are examples of this. Some of these functions accept a parameter called a format string that allows you to determine how the date is going to be displayed. The format string is a template in which various parts of the date are combined (using "format characters") to generate a date in the format specified.
  
 
For example, the format string:
 
For example, the format string:
第13行: 第24行:
 
Here is what each format character in the string above represents:  
 
Here is what each format character in the string above represents:  
  
* codel/code = Full name for day of the week (lower-case L).
+
* <code>l</code> = Full name for day of the week (lower-case L).
* codeF/code = Full name for the month.
+
* <code>F</code> = Full name for the month.
* codej/code = The day of the month.
+
* <code>j</code> = The day of the month.
* codeS/code = The English suffix for the day of the month (e.g ''st'', ''nd'' or ''th'' in the ''1st'', ''2nd'' or ''15th''.)
+
* <code>Y</code> = The year in 4 digits. (lower-case y gives the year's last 2 digits)
* codeY/code = The year in 4 digits. (lower-case y gives the year's last 2 digits)
 
 
: (Commas are read literally.)
 
: (Commas are read literally.)
  
WordPress is written in the programming language [[Glossary#PHP|PHP]].  Therefore, the date formatting functions in WordPress use PHP's built-in date formatting functions. You can use the [http://php.net/date table of date format characters on the PHP website] as a reference for building date format strings for use in WordPress.
+
WordPress is written in the programming language [[Glossary#PHP|PHP]].  The date formatting functions in WordPress use PHP's built-in date formatting functions. You can use the [http://php.net/date table of date format characters on the PHP website] as a reference for building date format strings for use in WordPress. Here is a table of some of the more useful items found there:
 +
 
 +
{| class="widefat"
 +
|-
 +
! style="background:#eee" colspan="3" | Day of Month
 +
|-
 +
| d
 +
| Numeric, with leading zeros
 +
| 01&ndash;31
 +
|-
 +
| j
 +
| Numeric, without leading zeros
 +
| 1&ndash;31
 +
|-
 +
| S
 +
| The English suffix for the day of the month
 +
| st, nd or th in the 1st, 2nd or 15th.
 +
|-
 +
! style="background:#eee" colspan="3" | Weekday
 +
|-
 +
| l
 +
| Full name &nbsp;(lowercase 'L')
 +
| Sunday &ndash; Saturday
 +
|-
 +
| D
 +
| Three letter name
 +
| Mon &ndash; Sun
 +
|-
 +
! style="background:#eee" colspan="3" | Month
 +
|-
 +
| m
 +
| Numeric, with leading zeros
 +
| 01&ndash;12
 +
|-
 +
| n
 +
| Numeric, without leading zeros
 +
| 1&ndash;12
 +
|-
 +
| F
 +
| Textual full
 +
| January &ndash; December
 +
|-
 +
| M
 +
| Textual three letters
 +
| Jan - Dec
 +
|-
 +
! style="background:#eee" colspan="3" | Year
 +
|-
 +
| Y
 +
| Numeric, 4 digits
 +
| Eg., 1999, 2003
 +
|-
 +
| y
 +
| Numeric, 2 digits
 +
| Eg., 99, 03
 +
|-
 +
! style="background:#eee" colspan="3" | Time
 +
|-
 +
| a
 +
| Lowercase
 +
| am, pm
 +
|-
 +
| A
 +
| Uppercase
 +
| AM, PM
 +
|-
 +
| g
 +
| Hour, 12-hour, without leading zeros
 +
| 1&ndash;12
 +
|-
 +
| h
 +
| Hour, 12-hour, with leading zeros
 +
| 01&ndash;12
 +
|-
 +
| G
 +
| Hour, 24-hour, without leading zeros
 +
| 0-23
 +
|-
 +
| H
 +
| Hour, 24-hour, with leading zeros
 +
| 00-23
 +
|-
 +
| i
 +
| Minutes, with leading zeros
 +
| 00-59
 +
|-
 +
| s
 +
| Seconds, with leading zeros
 +
| 00-59
 +
|-
 +
| T
 +
| Timezone abbreviation
 +
| Eg., EST, MDT ...
 +
|-
 +
! style="background:#eee" colspan="3" | Full Date/Time
 +
|-
 +
| c
 +
| ISO 8601
 +
| 2004-02-12T15:19:21+00:00
 +
|-
 +
| r
 +
| RFC 2822
 +
| Thu, 21 Dec 2000 16:01:07 +0200
 +
|}
 +
 
 +
===Examples===
  
p class=postmetadata altsmallThis entry was posted
+
Here are some examples of date format and result output.
on ?php the_time('l, F jS, Y') ? and is filed under
 
?php the_category(', ') ?./small/p
 
  
p class=postmetadata altsmallThis entry was posted  
+
* <code>F j, Y g:i a</code> - November 6, 2010 12:50 am
on Friday, September 24, 2004 and is filed under
+
* <code>F j, Y</code> - November 6, 2010
uWordPress/u and uWordPress Tips/u./small/p
+
* <code>F, Y</code> - November, 2010
 +
* <code>g:i a</code> - 12:50 am
 +
* <code>g:i:s a</code> - 12:50:48 am
 +
* <code>l, F jS, Y</code> - Saturday, November 6th, 2010
 +
* <code>M j, Y @ G:i</code> - Nov 6, 2010 @ 0:50
 +
* <code>Y/m/d \a\t g:i A</code> - 2010/11/06 at 12:50 AM
 +
* <code>Y/m/d \a\t g:ia</code> - 2010/11/06 at 12:50am
 +
* <code>Y/m/d g:i:s A</code> - 2010/11/06 12:50:48 AM
 +
* <code>Y/m/d</code> - 2010/11/06
 +
 
 +
Combined with <code>the_time()</code> template tag, the code below in the template file:
 +
 
 +
This entry was posted on <?php the_time('l, F jS, Y') ?> and is filed under <?php the_category(', ') ?>.
 +
 
 +
will be shown on your site as following:
 +
 +
This entry was posted on Friday, September 24th, 2004 and is filed under <u>WordPress</u> and <u>WordPress Tips</u>.
  
 
===Localization===
 
===Localization===
  
You might want to display the date in your own language:
+
To localize dates, use the [[Function_Reference/date_i18n|date_i18n()]] function.
 +
 
 +
You can probably safely localize these date format strings with the <code>__()</code>, <code>_e()</code>, etc. functions (demonstrated with <code>get_the_date(__(…))</code>):
  
code
+
===Escaping===
?php
 
setlocale(LC_ALL, 'nl_NL.ISO-8859-15@euro');
 
echo strftime('%A %d %B, %Y',strtotime(get_the_time('m/d/Y')));
 
?
 
/code
 
  
For this to work, your server has to be configured properly. The locale you are using (nl_NL.ISO-8859-15@euro) must be available.
+
You can escape custom characters using the <code>\letter</code> format.  For example you would escape the text <code>at</code> with  <code>\a\t</code>.
  
===Event, Calendar, and Time Plugins===
 
* [[Plugins/Calendar_Event|Calendar and Event Plugins]]
 
* [[Plugins/Time|Time oriented Plugins]]
 
  
 
[[Category:WordPress Lessons]]
 
[[Category:WordPress Lessons]]
 
[[Category:Design and Layout]]
 
[[Category:Design and Layout]]
 
[[Category:UI Link]]
 
[[Category:UI Link]]

2013年8月2日 (五) 04:19的最新版本

wordpress.org.cn

Customizing the Time and Date

Certain WordPress tag functions are used to display or return date and time information; the_date() and the_time() are examples of this. Some of these functions accept a parameter called a format string that allows you to determine how the date is going to be displayed. The format string is a template in which various parts of the date are combined (using "format characters") to generate a date in the format specified.

For example, the format string:

l, F j, Y

creates a date that look like this:

Friday, September 24, 2004

Here is what each format character in the string above represents:

  • l = Full name for day of the week (lower-case L).
  • F = Full name for the month.
  • j = The day of the month.
  • Y = The year in 4 digits. (lower-case y gives the year's last 2 digits)
(Commas are read literally.)

WordPress is written in the programming language PHP. The date formatting functions in WordPress use PHP's built-in date formatting functions. You can use the table of date format characters on the PHP website as a reference for building date format strings for use in WordPress. Here is a table of some of the more useful items found there:

Day of Month
d Numeric, with leading zeros 01–31
j Numeric, without leading zeros 1–31
S The English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
Weekday
l Full name  (lowercase 'L') Sunday – Saturday
D Three letter name Mon – Sun
Month
m Numeric, with leading zeros 01–12
n Numeric, without leading zeros 1–12
F Textual full January – December
M Textual three letters Jan - Dec
Year
Y Numeric, 4 digits Eg., 1999, 2003
y Numeric, 2 digits Eg., 99, 03
Time
a Lowercase am, pm
A Uppercase AM, PM
g Hour, 12-hour, without leading zeros 1–12
h Hour, 12-hour, with leading zeros 01–12
G Hour, 24-hour, without leading zeros 0-23
H Hour, 24-hour, with leading zeros 00-23
i Minutes, with leading zeros 00-59
s Seconds, with leading zeros 00-59
T Timezone abbreviation Eg., EST, MDT ...
Full Date/Time
c ISO 8601 2004-02-12T15:19:21+00:00
r RFC 2822 Thu, 21 Dec 2000 16:01:07 +0200

Examples

Here are some examples of date format and result output.

  • F j, Y g:i a - November 6, 2010 12:50 am
  • F j, Y - November 6, 2010
  • F, Y - November, 2010
  • g:i a - 12:50 am
  • g:i:s a - 12:50:48 am
  • l, F jS, Y - Saturday, November 6th, 2010
  • M j, Y @ G:i - Nov 6, 2010 @ 0:50
  • Y/m/d \a\t g:i A - 2010/11/06 at 12:50 AM
  • Y/m/d \a\t g:ia - 2010/11/06 at 12:50am
  • Y/m/d g:i:s A - 2010/11/06 12:50:48 AM
  • Y/m/d - 2010/11/06

Combined with the_time() template tag, the code below in the template file:

This entry was posted on <?php the_time('l, F jS, Y') ?> and is filed under <?php the_category(', ') ?>.

will be shown on your site as following:

This entry was posted on Friday, September 24th, 2004 and is filed under WordPress and WordPress Tips.

Localization

To localize dates, use the date_i18n() function.

You can probably safely localize these date format strings with the __(), _e(), etc. functions (demonstrated with get_the_date(__(…))):

Escaping

You can escape custom characters using the \letter format. For example you would escape the text at with \a\t.