设为首页 收藏本站
查看: 638|回复: 0

[经验分享] PHP日期时间相关date,time

[复制链接]

尚未签到

发表于 2017-4-2 14:10:48 | 显示全部楼层 |阅读模式
时间格式化函数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 个字符


stndrd 或者 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 新加)


例如:UTCGMTAtlantic/Azores


I


是否为夏令时


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


O


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


例如:+0200


P


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


例如:+02:00


T


本机所在的时区


例如:ESTMDT(【译者注】在 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


[+-]?[0-9]+


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


. [0-9]+


".21342", ".85"


hh


"0"?[1-9] | "1"[0-2]


"04", "7", "12"


HH


[01][0-9] | "2"[0-4]


"04", "7", "19"


meridian


[AaPp] .? [Mm] .? [\0\t ]


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


MM


[0-5][0-9]


"00", "12", "59"


II


[0-5][0-9]


"00", "12", "59"


space


[ \t]


 


tz


"("? [A-Za-z]{1,6} ")"? | [A-Z][a-z]+([_/][A-Z][a-z]+)+


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


tzcorrection


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


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


 
  或者二者综合:


Description



Formats



Examples



DD


"0" [0-9] | [1-2][0-9] | "3" [01]


"02", "12", "31"


doy


"00"[1-9] | "0"[1-9][0-9] | [1-2][0-9][0-9] | "3"[0-5][0-9] | "36"[0-6]


"36"[0-6] "000", "012", "366"


frac


. [0-9]+


".21342", ".85"


hh


"0"?[1-9] | "1"[0-2]


"04", "7", "12"


HH


[01][0-9] | "2"[0-4]


"04", "7", "19"


meridian


[AaPp] .? [Mm] .? [\0\t ]


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


ii


[0-5][0-9]


"04", "8", "59"


II


[0-5][0-9]


"04", "08", "59"


M


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


 


MM


[0-5][0-9]


"00", "12", "59"


space


[ \t]


 


ss


[0-5][0-9]


"04", "8", "59"


SS


[0-5][0-9]


"04", "08", "59"


W


"0"[1-9] | [1-4][0-9] | "5"[0-3]


"05", "17", "53"


tzcorrection


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


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


YY


[0-9]{4}


"2000", "2008", "1978"


 如:

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

  好

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359225-1-1.html 上篇帖子: PHP生成静态页的两种方法 下篇帖子: php中用fsockopen来跨站点请求
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表