📜  PHP | strftime()函数

📅  最后修改于: 2022-05-13 01:56:48.408000             🧑  作者: Mango

PHP | strftime()函数

strftime()函数是PHP中的一个内置函数,它根据区域设置格式化本地时间或日期,即,它为一个地方设置的位置格式化本地时间或日期。

句法:

strftime( $format, $timestamp ) 

参数:此函数接受上面提到的两个参数,如下所述:

  • $format:此参数定义日期和时间的格式,它是必须的参数。
  • $timestamp:可选的时间戳参数是一个整数Unix 时间戳,如果没有给出时间戳,则默认为当前本地时间。换句话说,它默认为 time() 的值。

返回值:它使用给定的 $timestamp 返回一个格式化为 $format 的字符串(如果明确提及,否则采用默认时间)。月份和工作日名称以及其他与语言相关的字符串尊重使用setlocale()设置的当前语言环境。

例子:

PHP


PHP


PHP


输出:

Thursday

格式:以下是可以添加到$format以获得所需输出的值。

  • 小时格式:
FORMATDESCRIPTIONEXAMPLE
%HTwo digit representation of the hour in 24-hour format it’s like train timing format.00 through 23
%kHour in 24-hour format, with a space preceding single digits0 through 23
%ITwo digit representation of the hour in 12-hour format01 through 12
%l12-hour format representation, with a space preceding single digits.1,2,3,.,12
%MTwo digit representation of the minute00 to 59
%pUPPER-CASE ‘AM’ or ‘PM’ based on the given time, 11:59 after will AM before will AM.AM for 00:31, PM for 22:23
%Plower-case ‘am’ or ‘pm’ based on the given time, 11:59 after will pm before will am.am for 00:31, pm for 22:23
%rSame as “%I:%M:%S %p”02:22:22 PM for 14:22:22
%RSame as “%H:%M”00:44 for 12:44 AM, 17:45for 5:45PM
%SIt,s two digit representation of seconds.00 to 59
%TSame as “%H:%M:%S”20:24:37 for 08:24:37 PM
%XIt represent preferred time representation without the date, based on locale.04:44:16 or 16:44:16
%zThe time zone offset. Not implemented as described on Windows. See below for more information.0500 for US Eastern Time
%ZIt represent time zone by reducing in 2 or three character.EST for Eastern Time
  • 时间和日期戳格式:
FORMATDESCRIPTIONEXAMPLE
%cPreferred date and time stamp based on locale.Tue Jan 5 00:55:25 2009 for January 5, 2009 at 12:55:25 AM
%DSame as “%m/%d/%y”01/05/09 for January 5, 2009
%FSame as “%Y-%m-%d” used in database datestamps.2009-01-05 for January 5, 2009
%sUnix Epoch Time timestamp same as the time() function.1525376494 for February 27, 2020 04:50:00 PM
%xPreferred date representation without the time based on locale.01/05/09 for January 5, 2009
  • 日期格式:
FORMATDESCRIPTIONEXAMPLE
%aIt reduced the textual representation of the day name.Sun,Mon, …
%AIt’s the textual representation of the full day name.Sunday,Monday,..
%dIt represent the day in double digits even day one represent like 01.01 to 31.
%eIt represent the day in single digits but day ten of the month present like 10.1 to 31.
%jIt represent the year in triple digits even day one represent like 001.001 to 366.
%uISO-8601 numeric representation of the day of the week.1 for Monday to 7 for Sunday.
%wIt is the numeric representation of the day of the week count start from 0.0 for Sunday to 6 for Saturday.
  • 周格式:
FORMATDESCRIPTIONEXAMPLE
%UNumber of week in the given year, starting from first Sunday as the first week13 there are 13th full week of the year,
%VISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week01 to 53 where 53 accounts for an overlapping week.
%WIt is numeric representation of week of the year, starting from first Monday as the first week like %U.46, 46th week of the year beginning with a Monday.
  • 月份格式:
FORMATDESCRIPTIONEXAMPLE
%bIt reduced the textual representation of the month name based on the locale.Jan, Feb, …
%BIt’s the textual representation of the full month name based on the locale.January, February, ..
%hIt reduced the textual representation of the month name based on the locale (an alias of %b).Jan, Feb, ..
%mIt represents the day in double digits even month one represents like 01.01 for January, 02 for February and so on.
  • 年份格式:
FORMATDESCRIPTIONEXAMPLE
%CIt represent the century in double digits (year divided by 100, truncated to an integer).19 for the 20th Century
%gIt represent the year in double digits by ISO-8601:1988 standards (see %V)09 for the week of January 6, 2009
%GIt is the full four-digit version of %g.2008 for the week of January 3, 2009
%yIt is the two digit representation of the year.09 for 2009, 79 for 1979
%YFour digit representation for the year2038
  • 其他格式:
FORMATDESCRIPTION
%nIt is a newline character (“\n”)
%tIt is a tab character (“\t”)
%%It is a literal percentage character (“%”)

下面的例子说明了 strftime() 在PHP中的应用:

示例 1:显示提供给它的日期和时间的简单程序。

PHP


输出:

03, January, 2004 09:34 PM

示例 2:此示例显示特定区域的时间(为此使用了一个附加函数setlocale()。要使 setlocale() 工作,您的服务器应支持语言环境。)

PHP


输出:

The current german time is 22:14:20 and the current english time is 10:14:20 PM

参考: https://www. PHP.net/manual/en/函数.strftime。 PHP