PHP strtotime函數詳解_PHP教程

來源:互聯網
上載者:User
先看手冊介紹:

strtotime — 將任何英文文本的日期時間描述解析為 Unix 時間戳記
格式:int strtotime ( string $time [, int $now ] )
  本函數預期接受一個包含美國英語日期格式的字串並嘗試將其解析為 Unix 時間戳記(自 January 1 1970 00:00:00 GMT 起的秒數),其值相對於 now 參數給出的時間,如果沒有提供此參數則用系統目前時間。
  本函數將使用 TZ 環境變數(如果有的話)來計算時間戳記。自 PHP 5.1.0 起有更容易的方法來定義時區用於所有的日期/時間函數。此過程在 date_default_timezone_get() 函數頁面中有說明。
Note : 如果給定的年份是兩位元字的格式,則其值 0-69 表示 2000-2069,70-100 表示 1970-2000。

參數
time
被解析的字串,格式根據 GNU » 日期輸入格式 的文法。在 PHP 5.0 之前,time 中不允許有毫秒數,自 PHP 5.0 起可以有但是會被忽略掉。
now
用來計算傳回值的時間戳記。 該參數預設值是目前時間time(),也可以設定為其他時間的時間戳記(我一直忽略的一個功能啊,慚愧)
傳回值: 成功則返回間戳,否則返回 FALSE 。在 PHP 5.1.0 之前本函數在失敗時返回 -1,後面版本返回false.

  strtotime的第一個參數可以是我們常見的英文時間格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以參數now為基準的時間描述,比如“+1 day”等等。


下面是後一種方式的可使用參數清單,其中“目前時間”是指strtotime第二個參數now的值,預設為目前時間
1.月,日英文名及其常用縮寫清單:
january,february,march,april,may,june,july,august,september,sept,october,november,december,
sunday,monday,tuesday,tues,wednesday,wednes,thursday,thur,thurs,friday,saturday

2.時間參數和祥細描述:
am : the time is before noon 上午
pm : the time is noon or later 下午
year: one year; for example, “next year” 年,比如“next year”代表明年
month : one month; for example, “last month” 月,比如“last month”代表上一月
fortnight : two weeks; for example, “a fortnight ago” 兩周,比如“a fortnight ago”代表兩周前
week : one week 周
day: a day 天
hour: an hour 小時
minute : a minute 分鐘
min : same as minute 同“minute”
second : a second 秒
sec : same as second 同“second”

3.相關和順序說明:
+n/-n :以目前時間算,加個減指定的時間,比如”+1 hour”是指目前時間加一小時
ago :time relative to now; such as “24 hours ago”  以目前時間往前算,比如”24 hours ago”代表“24小時前”
tomorrow : 24 hours later than the current date and time 以目前時間(包括日期和時間)為標準,明天同一時間
yesterday : 24 hours earlier than the current date and time 以目前時間(包括日期和時間)為標準,昨天同一時間
today : the current date and time 目前時間(包括日期和時間)
now : the current date and time 目前時間(包括日期和時間)
last : modifier meaning “the preceding”; for example, “last tuesday” 代表“上一個”,比如“last tuesday”代表“上周二同一時間”
this : the given time during the current day or the next occurrence of the given time; for example, “this 7am” gives the timestamp for 07:00 on the current day, while “this week” gives the timestamp for one week from the current time 當天的指定時間或下面一個時間段的時間戳記,比如“this 7am”給出當天7:00的時間戳記,而“this week”給出的是從目前時間開始的一整周的時間戳記,也就是目前時間(經本人測試:strtotime('this week')=strtotime('now'));
next : modifier meaning the current time value of the subject plus one; for example, “next hour” 目前時間加上指定的時間,比如“next hour”是指目前時間加上一小時,即加3600

//先到這,下面的還沒時間翻譯
first : ordinal modifier, esp. for months; for example, “May first” (actually, it's just the same as next)
third : see first (note that there is no “second” for ordinality, since that would conflict with the second time value)
fourth : see first
fifth : see first
sixth : see first
seventh : see first
eighth : see first
ninth : see first
tenth : see first
eleventh : see first
twelfth : see first

4.時區描述:
gmt : Greenwich Mean Time
ut : Coordinated Universal Time
utc : same as ut
wet : Western European Time
bst : British Summer Time
wat : West Africa Time
at : Azores Time
ast : Atlantic Standard Time
adt : Atlantic Daylight Time
est : Eastern Standard Time
edt : Eastern Daylight Time
cst : Central Standard Time
cdt : Central Daylight Time
mst : Mountain Standard Time
mdt : Mountain Daylight Time
pst : Pacific Standard Time
pdt : Pacific Daylight Time
yst : Yukon Standard Time
ydt : Yukon Daylight Time
hst : Hawaii Standard Time
hdt : Hawaii Daylight Time
cat : Central Alaska Time
akst : Alaska Standard Time
akdt : Alaska Daylight Time
ahst : Alaska-Hawaii Standard Time
nt : Nome Time
idlw : International Date Line West
cet : Central European Time
met : Middle European Time
mewt : Middle European Winter Time
mest : Middle European Summer Time
mesz : Middle European Summer Time
swt : Swedish Winter Time
sst : Swedish Summer Time
fwt : French Winter Time
fst : French Summer Time
eet : Eastern Europe Time, USSR Zone 1
bt : Baghdad Time, USSR Zone 2
zp4 : USSR Zone 3
zp5 : USSR Zone 4
zp6 : USSR Zone 5
wast : West Australian Standard Time
wadt : West Australian Daylight Time
cct : China Coast Time, USSR Zone 7
jst : Japan Standard Time, USSR Zone 8
east : Eastern Australian Standard Time
eadt : Eastern Australian Daylight Time
gst : Guam Standard Time, USSR Zone 9
nzt : New Zealand Time
nzst : New Zealand Standard Time
nzdt : New Zealand Daylight Time
idle : International Date Line East

在PHP中有個叫做strtotime的函數。strtotime 實現功能:擷取某個日期的時間戳記,或擷取某個時間的時間戳記。strtotime 將任何英文文本的日期時間描述解析為Unix時間戳記[將系統時間轉化成unix時間戳記]

一,擷取指定日期的unix時間戳記

strtotime("2009-1-22") 樣本如下:
1.echo strtotime("2009-1-22")
結果:1232553600
說明:返回2009年1月22日0點0分0秒時間戳記

二,擷取英文文本日期時間

樣本如下:
便於比較,使用date將當時間戳記與指定時間戳記轉換成系統時間
(1)列印明天此時的時間戳記strtotime("+1 day")
目前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
結果:2009-01-23 09:40:25
(2)列印昨天此時的時間戳記strtotime("-1 day")
目前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("-1 day"))
結果:2009-01-21 09:40:25
(3)列印下個星期此時的時間戳記strtotime("+1 week")
目前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("+1 week"))
結果:2009-01-29 09:40:25
(4)列印上個星期此時的時間戳記strtotime("-1 week")
目前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("-1 week"))
結果:2009-01-15 09:40:25
(5)列印指定下星期幾的時間戳記strtotime("next Thursday")
目前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("next Thursday"))
結果:2009-01-29 00:00:00
(6)列印指定上星期幾的時間戳記strtotime("last Thursday")
目前時間:
1.echo date("Y-m-d H:i:s",time())
結果:2009-01-22 09:40:25
指定時間:
1.echo date("Y-m-d H:i:s",strtotime("last Thursday"))
結果:2009-01-15 00:00:00
以上樣本可知,strtotime能將任何英文文本的日期時間描述解析為Unix時間戳記,我們結合mktime()或date()格式化日期時間擷取指定的時間戳記,實現所需要的日期時間。
希望通過本文的介紹後,你已經能掌握strtotime函數用法。

http://www.bkjia.com/PHPjc/321108.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321108.htmlTechArticle先看手冊介紹: strtotime — 將任何英文文本的日期時間描述解析為 Unix 時間戳記 格式:int strtotime ( string $time [, int $now ] ) 本函數預期接受一個...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.