对于 php>=8.1,有一个 strftime 兼容性包,位于 https://github.com/alphp/strftime
(PHP 4, PHP 5, PHP 7, PHP 8)
strftime — 根据区域设置格式化本地时间/日期
根据区域设置格式化时间和/或日期。月份和星期几名称以及其他与语言相关的字符串会遵循使用 setlocale() 设置的当前区域设置。
并非所有转换说明符都可能被您的 C 库支持,在这种情况下,它们将不被 PHP 的 strftime() 支持。此外,并非所有平台都支持负时间戳,因此您的日期范围可能仅限于不早于 Unix 纪元。这意味着 %e、%T、%R 和 %D(以及可能的其他) - 以及早于 Jan 1, 1970
的日期 - 在 Windows、一些 Linux 发行版和少数其他操作系统上将不起作用。对于 Windows 系统,可以在 » MSDN 上找到支持的转换说明符的完整概述。请改用 IntlDateFormatter::format() 方法。
format
format |
描述 | 示例返回值 |
---|---|---|
天 | --- | --- |
%a |
天的简短文本表示 | Sun 到 Sat |
%A |
天的完整文本表示 | Sunday 到 Saturday |
%d |
两位数的月份日(带前导零) | 01 到 31 |
%e |
月份日,一位数前面有空格。在 Windows 上未按描述实现。有关更多信息,请参见下文。 | 1 到 31 |
%j |
一年中的天数,三位数,带前导零 | 001 到 366 |
%u |
ISO-8601 星期几的数字表示 | 1 (星期一)到 7 (星期日) |
%w |
星期几的数字表示 | 0 (星期日)到 6 (星期六) |
周 | --- | --- |
%U |
给定年份的周数,以第一个星期日为第一周开始 | 13 (对于一年中的第 13 个完整周) |
%V |
ISO-8601:1988 给定年份的周数,以至少有 4 个工作日的第一个周开始,以星期一为一周的开始 | 01 到 53 (其中 53 占重叠周) |
%W |
一年中的周数的数字表示,以第一个星期一为第一周开始 | 46 (对于以星期一开始的第 46 周) |
月 | --- | --- |
%b |
简短的月份名称,基于区域设置 | Jan 到 Dec |
%B |
完整的月份名称,基于区域设置 | January 到 December |
%h |
简短的月份名称,基于区域设置(%b 的别名) | Jan 到 Dec |
%m |
月份的两位数表示 | 01 (一月)到 12 (十二月) |
年 | --- | --- |
%C |
世纪的两位数表示(年份除以 100,截断为整数) | 19 代表 20 世纪 |
%g |
根据 ISO-8601:1988 标准的年份两位数表示(参见 %V) | 示例:09 代表 2009 年 1 月 6 日的周 |
%G |
%g 的完整四位数版本 | 示例:2008 代表 2009 年 1 月 3 日的周 |
%y |
年份的两位数表示 | 示例:09 代表 2009 年,79 代表 1979 年 |
%Y |
年份的四位数表示 | 示例:2038 |
时间 | --- | --- |
%H |
24 小时制的小时两位数表示 | 00 到 23 |
%k |
24 小时制的小时,一位数前面有空格 | 0 到 23 |
%I |
12 小时制的小时两位数表示 | 01 到 12 |
%l(小写 'L') |
12 小时制的小时,一位数前面有空格 | 1 到 12 |
%M |
分钟的两位数表示 | 00 到 59 |
%p |
基于给定时间的“AM”或“PM”(大写) | 示例:AM 代表 00:31,PM 代表 22:23。确切结果取决于操作系统,它们也可能返回小写变体或带点的变体(例如 a.m. )。 |
%P |
基于给定时间的“am”或“pm”(小写) | 示例:am 代表 00:31,pm 代表 22:23。并非所有操作系统都支持。 |
%r |
与“%I:%M:%S %p”相同 | 示例:09:34:17 PM 代表 21:34:17 |
%R |
与“%H:%M”相同 | 示例:00:35 代表凌晨 12:35,16:44 代表下午 4:44 |
%S |
秒的两位数表示 | 00 到 59 |
%T |
与“%H:%M:%S”相同 | 示例:21:34:17 代表晚上 09:34:17 |
%X |
基于区域设置的首选时间表示,不含日期 | 示例:03:59:16 或 15:59:16 |
%z |
时区偏移量。在 Windows 上未按描述实现。有关更多信息,请参见下文。 | 示例:-0500 代表美国东部时间 |
%Z |
时区缩写。在 Windows 上未按描述实现。有关更多信息,请参见下文。 | 示例:EST 代表东部时间 |
时间和日期戳 | --- | --- |
%c |
基于区域设置的首选日期和时间戳 | 示例:Tue Feb 5 00:45:10 2009 代表 2009 年 2 月 5 日凌晨 12:45:10 |
%D |
与“%m/%d/%y”相同 | 示例:02/05/09 代表 2009 年 2 月 5 日 |
%F |
与“%Y-%m-%d”相同(通常用于数据库日期戳) | 示例:2009-02-05 代表 2009 年 2 月 5 日 |
%s |
Unix 纪元时间戳(与 time() 函数相同) | 示例:305815200 代表 1979 年 9 月 10 日上午 08:40:00 |
%x |
基于区域设置的首选日期表示,不含时间 | 示例:02/05/09 代表 2009 年 2 月 5 日 |
其他 | --- | --- |
%n |
换行符 ("\n") | --- |
%t |
制表符 ("\t") | --- |
%% |
百分号字符 ("%") 本身 | --- |
与 ISO-9899:1999 相反,Sun Solaris 以星期日为 1 开始。因此,%u
可能无法按本手册中所述工作。
仅限 Windows
此函数的 Windows 实现不支持 %e
修饰符。要实现此值,可以使用 %#d
修饰符。以下示例说明如何编写跨平台兼容函数。
%z
和 %Z
修饰符都返回时区名称,而不是偏移量或缩写。
仅限 macOS 和 musl:macOS 的此函数实现不支持 %P
修饰符。
timestamp
可选的 timestamp
参数是一个 int Unix 时间戳,如果省略 timestamp
或为 null
,则默认为当前本地时间。换句话说,它默认为 time() 的值。
返回一个根据 format
格式化的字符串,使用给定的 timestamp
或当前本地时间(如果未给出时间戳)。月份和星期几名称以及其他依赖于语言的字符串会尊重使用 setlocale() 设置的当前区域设置。如果 format
为空,包含不受支持的转换说明符,或者返回字符串的长度大于 4095
,则该函数返回 false
。
如果时区无效,则每次调用日期/时间函数都会生成 E_WARNING
。另请参见 date_default_timezone_set()
由于输出取决于底层的 C 库,因此某些转换说明符不受支持。在 Windows 上,提供未知转换说明符将导致 5 个 E_WARNING
消息并返回 false
。在其他操作系统上,您可能不会收到任何 E_WARNING
消息,并且输出可能包含未转换的转换说明符。
版本 | 描述 |
---|---|
8.0.0 |
timestamp 现在可以为空。 |
如果您在系统中安装了相应的区域设置,则此示例将起作用。
示例 #1 strftime() 区域设置示例
<?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");
?>
示例 #2 ISO 8601:1988 星期编号示例
<?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 */
// 输出: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";
// 输出: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";
// 输出: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";
// 输出: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 */
// 输出: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";
// 输出: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";
// 输出: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";
// 输出: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";
?>
示例 #3 使用 %e
修饰符的跨平台兼容示例
<?php
// 1 月 1 日:结果为:'%e%1%' (%%, e, %%, %e, %%)
$format = '%%e%%%e%%';
// 检查 Windows 以找到并正确替换 %e
// 修饰符
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
}
echo strftime($format);
?>
示例 #4 显示所有已知和未知格式
<?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'
注意: %G 和 %V 基于 ISO 8601:1988 星期编号,如果编号系统没有被完全理解,可能会产生意外(但正确)的结果。请参阅本手册页面的 %V 示例。
对于 php>=8.1,有一个 strftime 兼容性包,位于 https://github.com/alphp/strftime