strftime
strftime
(PHP 4, PHP 5, PHP 7)
strftime - 根据区域设置格式化本地时间/日期
描述
string strftime ( string $format [, int $timestamp = time() ] )
根据区域设置格式化时间和/或日期。月份和星期几名称以及其他依赖语言的字符串遵循setlocale()设置的当前语言环境。
并非所有的转换说明符都可能被C库支持,在这种情况下,它们不会被PHP的strftime()支持
。此外,并非所有平台都支持负时间戳,因此您的日期范围可能不会早于Unix时代。这意味着%e,%T,%R和%D(可能还有其他)以及1970
年1月1日
之前的日期- 不适用于Windows,某些Linux发行版以及其他一些操作系统。对于Windows系统,可以在»MSDN上找到支持的转换说明符的完整概述。
参数
format
格式 | 描述 | 示例返回的值 |
---|---|---|
天 | ||
%a | 一天的缩写文字表示 | 太阳到星期六 |
%A | 全天文本表示 | 周日到周六 |
%d | 本月的两位数日(前导零) | 01至31 |
%e | 本月的第几天,空格在单个数字之前。未按照Windows上的描述实施。请参阅下面的详细信息。 | 1至31 |
%j | 一年中的一天,3位数字带前导零 | 001至366 |
%u | ISO-8601星期几的数字表示 | 1(星期一)至7(星期日) |
%w | 星期几的数字表示 | 0(星期日)到6(星期六) |
Week | ||
%U | 给定年份的周数,从第一周的第一个星期日开始 | 13(一年的第13周) |
%V | ISO-8601:给定年份的1988年周数,从一年中的第一周开始,至少有4个工作日,周一为本周开始 | 01至53(其中53个重叠一周) |
%W | 一年中的第一周的数字表示,从第一个星期一开始 | 46(从周一开始的第46周) |
Month | ||
%b | 基于语言环境的缩写月份名称 | 1月至12月 |
%B | 完整的月份名称,基于语言环境 | 1月至12月 |
%h | 缩写月份名称,基于区域设置(%b的别名) | 1月至12月 |
%m | 两位数的月份表示 | 01(1月)到12(12月) |
Year | ||
%C | 本世纪的两位数表示(年份除以100,截断为整数) | 19世纪20年代 |
%g | 符合ISO-8601:1988标准的年份的两位数表示(参见%V) | 例如:2009年1月6日那周的09 |
%G | %g的完整四位数版本 | 例如:2009年1月3日这一周的2008年 |
%y | 年份的两位数表示 | 例如:09年为09年,1979年为79年 |
%Y | 一年四位数表示 | 例如:2038 |
Time | ||
%H | 24小时格式的两位数字表示小时 | 00至23日 |
%k | 小时24小时格式,前面有一位数字 | 0到23 |
%I | 12小时格式的两位数小时表示 | 01至12 |
%1(小写'L') | 12小时格式的小时,前面有一位数字 | 1至12 |
%M | 分钟的两位数字表示 | 00至59 |
%p | 基于给定时间的大写'AM'或'PM' | 例如:AM为00:31,PM为22:23 |
%P | 基于给定时间的小写'am'或'pm' | 例如:上午00:31,下午22:23 |
%r | 与“%I:%M:%S%p”相同 | 例如:09:34:17 PM 21:34:17 |
%R | 与“%H:%M”相同 | 例如:00:35为12:35 AM,16:44为4:44 PM |
%S | 第二位的两位数表示 | 00至59 |
%T | 与“%H:%M:%S”相同 | 例如:09:34:17 PM 21:34:17 |
%X | 基于语言环境的首选时间表示,不含日期 | 例如:03:59:16或15:59:16 |
%z | 时区偏移。未按照Windows上的描述实施。请参阅下面的详细信息。 | 例如:美国东部时间为-0500 |
%Z | 时区缩写。未按照Windows上的描述实施。请参阅下面的详细信息。 | 例如:东部时间的EST |
时间和日期邮票 | ||
%c | 基于区域设置的首选日期和时间戳记 | 示例:2009年2月5日星期二00:45:10 2009年2月5日上午12:45:10 |
%D | 与“%m/%d/%y”相同 | 例如:2009年2月5日02/05/09 |
%F | 与“%Y-%m-%d”(常用于数据库日期戳) | 例如:2009年2月5日2009-02-05 |
%s | Unix Epoch Time时间戳(与time()函数相同) | 例如:1979年9月10日上午08点40分00秒305815200 |
%x | 基于语言环境的首选日期表示,没有时间 | 例如:2009年2月5日02/05/09 |
杂 | ||
%n | 换行符(“\n”) | |
%t | 制表符(“\t”) | |
%% | 字面百分比字符(“%”) |
timestamp
可选timestamp
参数是一个整数Unix时间戳,默认为当前本地时间,如果timestamp
没有给出。换句话说,它默认为time()的值。
返回值
如果没有给出时间戳,返回一个字符串格式,根据format
给定的timestamp
或当前的本地时间。月份和星期几名称以及其他依赖语言的字符串遵循setlocale()设置的当前语言环境。
错误/异常
如果使用系统设置或TZ环境变量E_NOTICE
,则每次调用日期/时间函数都会生成一个如果时区无效的信息和/或a E_STRICT
或E_WARNING
message。另请参阅date_default_timezone_set()
由于输出依赖于底层C库,因此不支持某些转换说明符。在Windows上,提供未知的转换说明符将导致5条E_WARNING
消息并返回FALSE
。在其他操作系统上,您可能不会收到任何E_WARNING
消息,并且输出可能包含未转换的转换说明符。
Changelog
版本 | 描述 |
---|---|
5.1.0 | 现在发出E_STRICT和E_NOTICE时区错误。 |
示例
如果您的系统中安装了相应的语言环境,则此示例可以正常工作。
Example #1 strftime() locale examples
<?php
setlocale(LC_TIME, "C"
echo strftime("%A"
setlocale(LC_TIME, "fi_FI"
echo strftime(" in Finnish is %A,"
setlocale(LC_TIME, "fr_FR"
echo strftime(" in French %A and"
setlocale(LC_TIME, "de_DE"
echo strftime(" in German %A.\n"
?>
Example #2 ISO 8601:1988 week number example
<?php
/* December 2002 / January 2003
ISOWk M Tu W Thu F Sa Su
----- ----------------------------
51 16 17 18 19 20 21 22
52 23 24 25 26 27 28 29
1 30 31 1 2 3 4 5
2 6 7 8 9 10 11 12
3 13 14 15 16 17 18 19 */
// Outputs: 12/28/2002 - %V,%G,%Y = 52,2002,2002
echo "12/28/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/28/2002")) . "\n";
// Outputs: 12/30/2002 - %V,%G,%Y = 1,2003,2002
echo "12/30/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/30/2002")) . "\n";
// Outputs: 1/3/2003 - %V,%G,%Y = 1,2003,2003
echo "1/3/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2003")) . "\n";
// Outputs: 1/10/2003 - %V,%G,%Y = 2,2003,2003
echo "1/10/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/10/2003")) . "\n";
/* December 2004 / January 2005
ISOWk M Tu W Thu F Sa Su
----- ----------------------------
51 13 14 15 16 17 18 19
52 20 21 22 23 24 25 26
53 27 28 29 30 31 1 2
1 3 4 5 6 7 8 9
2 10 11 12 13 14 15 16 */
// Outputs: 12/23/2004 - %V,%G,%Y = 52,2004,2004
echo "12/23/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/23/2004")) . "\n";
// Outputs: 12/31/2004 - %V,%G,%Y = 53,2004,2004
echo "12/31/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/31/2004")) . "\n";
// Outputs: 1/2/2005 - %V,%G,%Y = 53,2004,2005
echo "1/2/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/2/2005")) . "\n";
// Outputs: 1/3/2005 - %V,%G,%Y = 1,2005,2005
echo "1/3/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2005")) . "\n";
?>
Example #3 Cross platform compatible example using the
%e
modifier
<?php
// Jan 1: results in: '%e%1%' (%%, e, %%, %e, %%)
$format = '%%e%%%e%%';
// Check for Windows to find and replace the %e
// modifier correctly
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format
}
echo strftime($format
?>
Example #4 Display all known and unknown formats.
<?php
// Describe the formats.
$strftimeFormats = array(
'A' => 'A full textual representation of the day',
'B' => 'Full month name, based on the locale',
'C' => 'Two digit representation of the century (year divided by 100, truncated to an integer)',
'D' => 'Same as "%m/%d/%y"',
'E' => '',
'F' => 'Same as "%Y-%m-%d"',
'G' => 'The full four-digit version of %g',
'H' => 'Two digit representation of the hour in 24-hour format',
'I' => 'Two digit representation of the hour in 12-hour format',
'J' => '',
'K' => '',
'L' => '',
'M' => 'Two digit representation of the minute',
'N' => '',
'O' => '',
'P' => 'lower-case "am" or "pm" based on the given time',
'Q' => '',
'R' => 'Same as "%H:%M"',
'S' => 'Two digit representation of the second',
'T' => 'Same as "%H:%M:%S"',
'U' => 'Week number of the given year, starting with the first Sunday as the first week',
'V' => 'ISO-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 week',
'W' => 'A numeric representation of the week of the year, starting with the first Monday as the first week',
'X' => 'Preferred time representation based on locale, without the date',
'Y' => 'Four digit representation for the year',
'Z' => 'The time zone offset/abbreviation option NOT given by %z (depends on operating system)',
'a' => 'An abbreviated textual representation of the day',
'b' => 'Abbreviated month name, based on the locale',
'c' => 'Preferred date and time stamp based on local',
'd' => 'Two-digit day of the month (with leading zeros)',
'e' => 'Day of the month, with a space preceding single digits',
'f' => '',
'g' => 'Two digit representation of the year going by ISO-8601:1988 standards (see %V)',
'h' => 'Abbreviated month name, based on the locale (an alias of %b)',
'i' => '',
'j' => 'Day of the year, 3 digits with leading zeros',
'k' => 'Hour in 24-hour format, with a space preceding single digits',
'l' => 'Hour in 12-hour format, with a space preceding single digits',
'm' => 'Two digit representation of the month',
'n' => 'A newline character ("\n")',
'o' => '',
'p' => 'UPPER-CASE "AM" or "PM" based on the given time',
'q' => '',
'r' => 'Same as "%I:%M:%S %p"',
's' => 'Unix Epoch Time timestamp',
't' => 'A Tab character ("\t")',
'u' => 'ISO-8601 numeric representation of the day of the week',
'v' => '',
'w' => 'Numeric representation of the day of the week',
'x' => 'Preferred date representation based on locale, without the time',
'y' => 'Two digit representation of the year',
'z' => 'Either the time zone offset from UTC or the abbreviation (depends on operating system)',
'%' => 'A literal percentage character ("%")',
// Results.
$strftimeValues = array(
// Evaluate the formats whilst suppressing any errors.
foreach($strftimeFormats as $format => $description){
if (False !== ($value = @strftime("%{$format}"))){
$strftimeValues[$format] = $value;
}
}
// Find the longest value.
$maxValueLength = 2 + max(array_map('strlen', $strftimeValues)
// Report known formats.
foreach($strftimeValues as $format => $value){
echo "Known format : '{$format}' = ", str_pad("'{$value}'", $maxValueLength), " ( {$strftimeFormats[$format]} )\n";
}
// Report unknown formats.
foreach(array_diff_key($strftimeFormats, $strftimeValues) as $format => $description){
echo "Unknown format : '{$format}' ", str_pad(' ', $maxValueLength), ($description ? " ( {$description} )" : ''), "\n";
}
?>
上面的例子会输出类似于:
Known format : 'A' = 'Friday' ( A full textual representation of the day )
Known format : 'B' = 'December' ( Full month name, based on the locale )
Known format : 'H' = '11' ( Two digit representation of the hour in 24-hour format )
Known format : 'I' = '11' ( Two digit representation of the hour in 12-hour format )
Known format : 'M' = '24' ( Two digit representation of the minute )
Known format : 'S' = '44' ( Two digit representation of the second )
Known format : 'U' = '48' ( Week number of the given year, starting with the first Sunday as the first week )
Known format : 'W' = '48' ( A numeric representation of the week of the year, starting with the first Monday as the first week )
Known format : 'X' = '11:24:44' ( Preferred time representation based on locale, without the date )
Known format : 'Y' = '2010' ( Four digit representation for the year )
Known format : 'Z' = 'GMT Standard Time' ( The time zone offset/abbreviation option NOT given by %z (depends on operating system) )
Known format : 'a' = 'Fri' ( An abbreviated textual representation of the day )
Known format : 'b' = 'Dec' ( Abbreviated month name, based on the locale )
Known format : 'c' = '12/03/10 11:24:44' ( Preferred date and time stamp based on local )
Known format : 'd' = '03' ( Two-digit day of the month (with leading zeros) )
Known format : 'j' = '337' ( Day of the year, 3 digits with leading zeros )
Known format : 'm' = '12' ( Two digit representation of the month )
Known format : 'p' = 'AM' ( UPPER-CASE "AM" or "PM" based on the given time )
Known format : 'w' = '5' ( Numeric representation of the day of the week )
Known format : 'x' = '12/03/10' ( Preferred date representation based on locale, without the time )
Known format : 'y' = '10' ( Two digit representation of the year )
Known format : 'z' = 'GMT Standard Time' ( Either the time zone offset from UTC or the abbreviation (depends on operating system) )
Known format : '%' = '%' ( A literal percentage character ("%") )
Unknown format : 'C' ( Two digit representation of the century (year divided by 100, truncated to an integer) )
Unknown format : 'D' ( Same as "%m/%d/%y" )
Unknown format : 'E'
Unknown format : 'F' ( Same as "%Y-%m-%d" )
Unknown format : 'G' ( The full four-digit version of %g )
Unknown format : 'J'
Unknown format : 'K'
Unknown format : 'L'
Unknown format : 'N'
Unknown format : 'O'
Unknown format : 'P' ( lower-case "am" or "pm" based on the given time )
Unknown format : 'Q'
Unknown format : 'R' ( Same as "%H:%M" )
Unknown format : 'T' ( Same as "%H:%M:%S" )
Unknown format : 'V' ( ISO-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 week )
Unknown format : 'e' ( Day of the month, with a space preceding single digits )
Unknown format : 'f'
Unknown format : 'g' ( Two digit representation of the year going by ISO-8601:1988 standards (see %V) )
Unknown format : 'h' ( Abbreviated month name, based on the locale (an alias of %b) )
Unknown format : 'i'
Unknown format : 'k' ( Hour in 24-hour format, with a space preceding single digits )
Unknown format : 'l' ( Hour in 12-hour format, with a space preceding single digits )
Unknown format : 'n' ( A newline character ("\n") )
Unknown format : 'o'
Unknown format : 'q'
Unknown format : 'r' ( Same as "%I:%M:%S %p" )
Unknown format : 's' ( Unix Epoch Time timestamp )
Unknown format : 't' ( A Tab character ("\t") )
Unknown format : 'u' ( ISO-8601 numeric representation of the day of the week )
Unknown format : 'v'
笔记
注意
:如果未完全理解编号系统,则基于ISO 8601:1988星期编号的%G和%V可能会给出意外(尽管是正确的)结果。请参阅本手册页面中的%V示例。
另请参阅
- setlocale() - 设置区域设置信息
- mktime() - 获取某个日期的Unix时间戳
- strptime() - 解析用strftime生成的时间/日期
- gmstrftime() - 根据区域设置格式化GMT/UTC时间/日期
- » Open Group specification of
strftime()
← mktime
strptime →