4591566 发表于 2017-4-2 14:10:48

PHP日期时间相关date,time

时间格式化函数date();

string date ( string $format [, int $timestamp ] )
 时间戳参数不传递则为当前时间。


格式化方式指定:

echo "今天:".date("Y-m-d")."<br>";
 



format字符



说明



返回值例子








---



---





d



月份中的第几天,有前导零的 2 位数字



01 到 31





D



星期中的第几天,文本表示,3 个字母



Mon 到 Sun





j



月份中的第几天,没有前导零



1 到 31





l(“L”的小写字母)



星期几,完整的文本格式



Sunday 到 Saturday





N



ISO-8601 格式数字表示的星期中的第几天(PHP 5.1.0 新加)



1(表示星期一)到 7(表示星期天)





S



每月天数后面的英文后缀,2 个字符



st,nd,rd 或者 th。可以和 j 一起用





w



星期中的第几天,数字表示



0(表示星期天)到 6(表示星期六)





z



年份中的第几天



0 到 365





星期



---



---





W



ISO-8601 格式年份中的第几周,每周从星期一开始(PHP 4.1.0 新加的)



例如:42(当年的第 42 周)









---



---





F



月份,完整的文本格式,例如 January 或者 March



January 到 December





m



数字表示的月份,有前导零



01 到 12





M



三个字母缩写表示的月份



Jan 到 Dec





n



数字表示的月份,没有前导零



1 到 12





t



给定月份所应有的天数



28 到 31









---



---





L



是否为闰年



如果是闰年为 1,否则为 0





o



ISO-8601 格式年份数字。这和 Y 的值相同,只除了如果 ISO 的星期数(W)属于前一年或下一年,则用那一年。(PHP 5.1.0 新加)



Examples: 1999 or 2003





Y



4 位数字完整表示的年份



例如:1999 或 2003





y



2 位数字表示的年份



例如:99 或 03





时间



---



---





a



小写的上午和下午值



am 或 pm





A



大写的上午和下午值



AM 或 PM





B



Swatch Internet 标准时



000 到 999





g



小时,12 小时格式,没有前导零



1 到 12





G



小时,24 小时格式,没有前导零



0 到 23





h



小时,12 小时格式,有前导零



01 到 12





H



小时,24 小时格式,有前导零



00 到 23





i



有前导零的分钟数



00 到 59>





s



秒数,有前导零



00 到 59>





时区



---



---





e



时区标识(PHP 5.1.0 新加)



例如:UTC,GMT,Atlantic/Azores





I



是否为夏令时



如果是夏令时为 1,否则为 0





O



与格林威治时间相差的小时数



例如:+0200





P



与格林威治时间(GMT)的差别,小时和分钟之间有冒号分隔(PHP 5.1.3 新加)



例如:+02:00





T



本机所在的时区



例如:EST,MDT(【译者注】在 Windows 下为完整文本格式,例如“Eastern Standard Time”,中文版会显示“中国标准时间”)。





Z



时差偏移量的秒数。UTC 西边的时区偏移量总是负的,UTC 东边的时区偏移量总是正的。



-43200 到 43200





完整的日期/时间



---



---





c



ISO 8601 格式的日期(PHP 5 新加)



2004-02-12T15:19:21+00:00





r



RFC 822 格式的日期



例如:Thu, 21 Dec 2000 16:01:07 +0200





U



从 Unix 纪元(January 1 1970 00:00:00 GMT)开始至今的秒数



参见 time()



 



strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳

int strtotime ( string $time [, int $now = time() ] )
 date()与strtotime()同时使用,就可以方便得到昨天,前天,明天等相关时间格式化方式:


如:

<?php
echo "昨天:".date("Y-m-d",strtotime("yesterday")), "<br>";      
?>
 



Description



Format




dayname



'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun'





daytext



'weekday' | 'weekdays'





number



[+-]?+





ordinal



'first' | 'second' | 'third' | 'fourth' | 'fifth' | 'sixth' | 'seventh' | 'eighth' | 'ninth' | 'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last' | 'previous' | 'this'





reltext



'next' | 'last' | 'previous' | 'this'





space



[ \t]+





unit



(('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | 'weeks' | daytext





<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>




Description



Examples




Midnight of yesterday



"yesterday 14:00"





The time is set to 00:00:00



 





The time is set to 00:00:00



 





Now - this is simply ignored



 





The time is set to 12:00:00



"yesterday noon"





Midnight of tomorrow



 





15 minutes past the specified hour



"back of 7pm", "back of 15"





15 minutes before the specified hour



"front of 5am", "front of 23"





Sets the day of the first of the current month. This phrase is best used together with a month name following it.



"first day of January 2008"





Sets the day to the last day of the current month. This phrase is best used together with a month name following it.



"last day of next month"





Calculates the x-th week day of the current month.



"first sat of July 2008"





Calculates the last week day of the current month.



"last sat of July 2008"





Handles relative time items where the value is a number.



"+5 weeks", "12 day", "-7 weekdays"





Handles relative time items where the value is text.



"fifth day", "second month"





Negates all the values of previously found relative time items.



"2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago ago"





Moves to the next day of this name.



"Monday"





Handles the special format "weekday + last/this/next week".



"Monday next week"



<?php
echo "今天:".date("Y-m-d")."<br>";   
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";   
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";   
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";   
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";   
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";   
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";   
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";   
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";      
?>

  也可以用格式化的确定日期的字符串:

<?php echo date("Y-m-d", strtotime("2011-W17-6")) . "\n"; ?>
  如:




mm "/" dd



"5/12", "10/27"





mm "/" dd "/" y



"12/22/78", "1/17/2006", "1/17/6"





YY "/" mm "/" dd



"2008/6/30", "1978/12/22"





YY "-" mm



"2008-6", "2008-06", "1978-12"





y "-" mm "-" dd



"2008-6-30", "78-12-22", "8-6-21"





dd [.\t-] mm [.-] YY



"30-6-2008", "22.12\t1978"





dd [.\t] mm "." yy



"30.6.08", "22\t12\t78"





dd ([ \t.-])* m ([ \t.-])* y



"30-June 2008", "22DEC78", "14 III 1879"





m ([ \t.-])* YY



"June 2008", "DEC1978", "March 1879"





YY ([ \t.-])* m



"2008 June", "1978-XII", "1879.MArCH"





m ([ .\t-])* dd [,.stndrh\t ]+y



"July 1st, 2008", "April 17, 1790", "May.9,78"





m ([ .\t-])* dd [,.stndrh\t ]*



"July 1st,", "Apr 17", "May.9"





d ([ .\t-])* m



"1 July", "17 Apr", "9.May"





M "-" DD "-" y



"May-09-78", "Apr-17-1790"





y "-" M "-" DD



"78-Dec-22", "1814-MAY-17"





YY



"1978", "2008"





m



"March", "jun", "DEC"



  也可以用格式化的确定时间的字符串:




Description



Formats



Examples




frac



. +



".21342", ".85"





hh



"0"? | "1"



"04", "7", "12"





HH



| "2"



"04", "7", "19"





meridian



.? .? [\0\t ]



"A.m.", "pM", "am."





MM







"00", "12", "59"





II







"00", "12", "59"





space



[ \t]



 





tz



"("? {1,6} ")"? | +(+)+



"CEST", "Europe/Amsterdam", "America/Indiana/Knox"





tzcorrection



"GMT"? [+-] hh ":"? MM?



"+0400", "GMT-07:00", "-07:00"



 
  或者二者综合:




Description



Formats



Examples




DD



"0" | | "3"



"02", "12", "31"





doy



"00" | "0" | | "3" | "36"



"36" "000", "012", "366"





frac



. +



".21342", ".85"





hh



"0"? | "1"



"04", "7", "12"





HH



| "2"



"04", "7", "19"





meridian



.? .? [\0\t ]



"A.m.", "pM", "am."





ii







"04", "8", "59"





II







"04", "08", "59"





M



'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec'



 





MM







"00", "12", "59"





space



[ \t]



 





ss







"04", "8", "59"





SS







"04", "08", "59"





W



"0" | | "5"



"05", "17", "53"





tzcorrection



"GMT"? [+-] hh ":"? MM?



"+0400", "GMT-07:00", "-07:00"





YY



{4}



"2000", "2008", "1978"



 如:

<?php echo date("Y-m-d H:i:s", strtotime("2013-5-16 15:03:23")) . "\n"; ?>

  好
页: [1]
查看完整版本: PHP日期时间相关date,time