php strftime函数的详细用法
定义和用法
strftime()函数根据区域设置格式化本地时间/日期。
语法
strftime(format,timestamp)参数描述
format可选。规定如何返回结果。
timestamp可选。
提示和注释
提示:与gmstrftime()的行为相同,不同的是返回时间是本地时间。
例子
输出strftime()和gmstrftime()的结果:
strftime()函数可以把YYYY-MM-DDHH:MM:SS格式的日期字符串转换成其它形式的字符串。
strftime()的语法是strftime(格式,日期/时间,修正符,修正符,...)
它可以用以下的符号对日期和时间进行格式化:
%a | 当前区域星期几的简写 | Sun 到 Sat |
%A | 当前区域星期几的全称 | Sunday 到 Saturday |
%d | 月份中的第几天,十进制数字(范围从01到31) | 01 到 31 |
%e | 月份中的第几天,十进制数字,一位的数字前会加上一个空格(范围从'1'到'31')在Windows上尚未按描述实现。更多信息见下方。 | 1 到 31 |
%j | 年份中的第几天,带前导零的三位十进制数(范围从001到366) | 001 到 366 |
%u | 符合ISO-8601星期几的十进制数表达[1,7],1表示星期一 | 1 (星期一)到 7 (星期日) |
%w | 星期中的第几天,星期天为0 | 0 (星期天)到 6 (星期六) |
周 | --- | --- |
%U | 本年的第几周,从第一周的第一个星期天作为第一天开始 | 13 (forthe13thfullweekoftheyear) |
%V | %V-本年第几周的ISO-8601:1988格式,范围从01到53,第1周是本年第一个至少还有4天的星期,星期一作为每周的第一天。(用%G或者%g作为指定时间戳相应周数的年份组成。) | 01 到 53 (where53accountsforanoverlappingweek) |
%W | 本年的第几周数,从第一周的第一个星期一作为第一天开始 | 46 (forthe46thweekoftheyearbeginningwithaMonday) |
月 | --- | --- |
%b | 当前区域月份的简写 | Jan 到 Dec |
%B | 当前区域月份的全称 | January 到 December |
%h | 当前区域月份的简写(%b的别名) | Jan 到 Dec |
%m | 两位数的月份 | 01 (是一月份)到 12 (是十二月份) |
年 | --- | --- |
%C | 两位数显示世纪(年份除以100,截成整数) | 19 是20世纪 |
%g | 2位数的年份,符合ISO-8601:1988星期数(参见%V)。和%V的格式和值一样,只除了如果ISO星期数属于前一年或者后一年,则使用那一年。 | 比如:2009年1月6日那一周是 09。 |
%G | %g的完整四位数版本 | 比如:2009年1月3日那一周是 2008. |
%y | 两位数显示年份 | 比如: 09 是2009,79 是1979 |
%Y | 四位数显示年份 | 比如: 2038 |
时间 | --- | --- |
%H | 以24小时格式显示两位小时数 | 00 到 23 |
%I | 以12小时格式显示两位小时数 | 01 到 12 |
%l('L'的小写) | 以12小时格式显示小时数,单个数字前含空格 | 1 到 12 |
%M | 两位的分钟数 | 00 到 59 |
%p | 指定时间的大写“AM”或“PM” | 比如:00:31是 AM ,22:23是PM |
%P | 指定时间的小写“am”或“pm” | 比如:00:31是 am ,22:23是pm |
%r | 和"%I:%M:%S%p"一样 | 比如:21:34:17是 09:34:17PM |
%R | 和"%H:%M"一样 | 比如:12:35AM是 00:35,4:44PM是 16:44 |
%S | 两位数字表示秒 | 00 到 59 |
%T | 和"%H:%M:%S"一样 | 比如:09:34:17PM是 21:34:17 |
%X | 当前区域首选的时间表示法,不包括日期 | 例如: 03:59:16 或 15:59:16 |
%z | 从UTC的时区偏移或简写(由操作系统决定) | 比如:东部时间是 -0500 或 EST |
%Z | %z没有给出的UTC的时区偏移或简写(由操作系统决定) | 比如: -0500 或 EST 是东部时间 |
时间和日期戳 | --- | --- |
%c | 当前区域首选的日期时间表达 | 比如:2009年2月5日上午12:45:10是 TueFeb500:45:102009 |
%D | 和"%m/%d/%y"一样 | 比如:2009年2月5日是 02/05/09 |
%F | Sameas"%Y-%m-%d"(commonlyusedindatabasedatestamps) | 比如:2009年2月5日是 2009-02-05 |
%s | Unix纪元的时间戳(和 time() 函数一样) | 比如:1979年9月10日上午8点40分00秒是 305815200 |
%x | 当前区域首选的时间表示法,不包括时间 | 比如:2009年2月5日是 02/05/09 |
其他 | --- | --- |
%n | 换行符("\n") | --- |
%t | Tab字符("\t") | --- |
%% | 文字上的百分字符("%") |
这个参数的最大长度是1023个字符。
Warning
尽管ISO9889:1999(当前的C标准)明确指出一周从星期一开始,但是SunSolaris的一周似乎从星期天开始并作为1。所以%u的结果也许不会和手册里描述得一样。
Warning
仅针对Windows:这个函数里%e修饰符修饰符还不能支持Windows。为了得到这个值可以用%#d修饰符来代替。下例说明了如何写一个跨平台支持的函数。
Warning
仅针对MacOSX:这个函数里%P修饰符还不能支持MacOSX。
timestamp
可选的timestamp参数是一个integer的Unix时间戳,如未指定,参数值默认为当前本地时间。也就是说,其值默认为time()的返回值。
strftime()的用法举例如下:
selectstrftime('%Y.%m.%d%H:%M:%S','now','localtime');
结果:2018.06.2121:41:09
其实更好的用法是这样的,比如统,本月或本年的支出:
selectstrftime('%Y-%m',日期)as月份,sum(支出)as月支出from流水帐groupby月份;
Example#1strftime()localeexamples
Example#2ISO8601:1988weeknumberexample
Example#3Crossplatformcompatibleexampleusingthe%emodifier
Example#4Displayallknownandunknownformats.
'Afulltextualrepresentationoftheday', 'B'=>'Fullmonthname,basedonthelocale', 'C'=>'Twodigitrepresentationofthecentury(yeardividedby100,truncatedtoaninteger)', 'D'=>'Sameas"%m/%d/%y"', 'E'=>'', 'F'=>'Sameas"%Y-%m-%d"', 'G'=>'Thefullfour-digitversionof%g', 'H'=>'Twodigitrepresentationofthehourin24-hourformat', 'I'=>'Twodigitrepresentationofthehourin12-hourformat', 'J'=>'', 'K'=>'', 'L'=>'', 'M'=>'Twodigitrepresentationoftheminute', 'N'=>'', 'O'=>'', 'P'=>'lower-case"am"or"pm"basedonthegiventime', 'Q'=>'', 'R'=>'Sameas"%H:%M"', 'S'=>'Twodigitrepresentationofthesecond', 'T'=>'Sameas"%H:%M:%S"', 'U'=>'Weeknumberofthegivenyear,startingwiththefirstSundayasthefirstweek', 'V'=>'ISO-8601:1988weeknumberofthegivenyear,startingwiththefirstweekoftheyearwithatleast4weekdays,withMondaybeingthestartoftheweek', 'W'=>'Anumericrepresentationoftheweekoftheyear,startingwiththefirstMondayasthefirstweek', 'X'=>'Preferredtimerepresentationbasedonlocale,withoutthedate', 'Y'=>'Fourdigitrepresentationfortheyear', 'Z'=>'Thetimezoneoffset/abbreviationoptionNOTgivenby%z(dependsonoperatingsystem)', 'a'=>'Anabbreviatedtextualrepresentationoftheday', 'b'=>'Abbreviatedmonthname,basedonthelocale', 'c'=>'Preferreddateandtimestampbasedonlocal', 'd'=>'Two-digitdayofthemonth(withleadingzeros)', 'e'=>'Dayofthemonth,withaspaceprecedingsingledigits', 'f'=>'', 'g'=>'TwodigitrepresentationoftheyeargoingbyISO-8601:1988standards(see%V)', 'h'=>'Abbreviatedmonthname,basedonthelocale(analiasof%b)', 'i'=>'', 'j'=>'Dayoftheyear,3digitswithleadingzeros', 'k'=>'Hourin24-hourformat,withaspaceprecedingsingledigits', 'l'=>'Hourin12-hourformat,withaspaceprecedingsingledigits', 'm'=>'Twodigitrepresentationofthemonth', 'n'=>'Anewlinecharacter("\n")', 'o'=>'', 'p'=>'UPPER-CASE"AM"or"PM"basedonthegiventime', 'q'=>'', 'r'=>'Sameas"%I:%M:%S%p"', 's'=>'UnixEpochTimetimestamp', 't'=>'ATabcharacter("\t")', 'u'=>'ISO-8601numericrepresentationofthedayoftheweek', 'v'=>'', 'w'=>'Numericrepresentationofthedayoftheweek', 'x'=>'Preferreddaterepresentationbasedonlocale,withoutthetime', 'y'=>'Twodigitrepresentationoftheyear', 'z'=>'EitherthetimezoneoffsetfromUTCortheabbreviation(dependsonoperatingsystem)', '%'=>'Aliteralpercentagecharacter("%")', ); //Results. $strftimeValues=array(); //Evaluatetheformatswhilstsuppressinganyerrors. foreach($strftimeFormatsas$format=>$description){ if(False!==($value=@strftime("%{$format}"))){ $strftimeValues[$format]=$value; } } //Findthelongestvalue. $maxValueLength=2+max(array_map('strlen',$strftimeValues)); //Reportknownformats. foreach($strftimeValuesas$format=>$value){ echo"Knownformat:'{$format}'=",str_pad("'{$value}'",$maxValueLength),"({$strftimeFormats[$format]})\n"; } //Reportunknownformats. foreach(array_diff_key($strftimeFormats,$strftimeValues)as$format=>$description){ echo"Unknownformat:'{$format}'",str_pad('',$maxValueLength),($description?"({$description})":''),"\n"; } ?>
下面是其他网友的补充
phpstrftime()的使用
stringstrftime(stringformat[,inttimestamp=time()])
返回用给定的格式字串对给出的timestamp进行格式输出后的字符串。如果没有给出时间戳则用当前的本地时间。
Example1:
本年中的第·31·周
Example2:
現在時間是:20.July201517:01:26
Example3:
日期時間格式化為:15.October201800:00:00