strftime

(PHP 3, PHP 4 )

strftime --  根据区域设置格式化本地时间/日期

说明

string strftime ( string format [, int timestamp])

返回用给定的格式字串对给出的 timestamp 进行格式输出后的字符串。如果没有给出时间戳则用当前的本地时间。月份和星期几以及其它和语言有关的字符串写法和用 setlocale() 设定的当前的区域有关。

格式字串能识别下列转换标记:

注: 可能不是所有的转换标记都被 C 库文件支持,这种情况下 PHP 的 strftime() 也不支持。这意味着例如 %e, %T,%R 和 %D(可能更多)在 Windows 下不起作用。

例子 1. strftime() 区域的例子

<?php
setlocale (LC_TIME, "C");
print (strftime ("%A in Finnish is "));
setlocale (LC_TIME, "fi_FI");
print (strftime ("%A, in French "));
setlocale (LC_TIME, "fr_FR");
print (strftime ("%A and in German "));
setlocale (LC_TIME, "de_DE");
print (strftime ("%A.\n"));
?>
本例在你的系统中安装有各自的区域设置后才能工作。

注: %G 和 %V,如果数字编号系统未能充分理解,基于 ISO 8601:1988 的星期数可能得出未预期的结果。见上面的 %V 和以下的例子。

例子 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 
print "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 
print "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 
print "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 
print "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 
print "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 
print "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 
print "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 
print "1/3/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2005")) . "\n"; 
  
?>

参见 setlocale()mktime() 以及 Open Group specification of strftime()