PHP strtotime函數(shù)詳解
更新時(shí)間:2009年12月18日 22:04:41 作者:
strtotime函數(shù)是一個(gè)很好的函數(shù),靈活的運(yùn)用它,會給你的工作帶來不少方便.但PHP的手冊中卻對此函數(shù)的參數(shù)沒作太多介紹,對些函數(shù)的其他介紹也非常少。
先看手冊介紹:
strtotime — 將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳
格式:int strtotime ( string $time [, int $now ] )
本函數(shù)預(yù)期接受一個(gè)包含美國英語日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對于 now 參數(shù)給出的時(shí)間,如果沒有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間。
本函數(shù)將使用 TZ 環(huán)境變量(如果有的話)來計(jì)算時(shí)間戳。自 PHP 5.1.0 起有更容易的方法來定義時(shí)區(qū)用于所有的日期/時(shí)間函數(shù)。此過程在 date_default_timezone_get() 函數(shù)頁面中有說明。
Note : 如果給定的年份是兩位數(shù)字的格式,則其值 0-69 表示 2000-2069,70-100 表示 1970-2000。
參數(shù)
time
被解析的字符串,格式根據(jù) GNU » 日期輸入格式 的語法。在 PHP 5.0 之前,time 中不允許有毫秒數(shù),自 PHP 5.0 起可以有但是會被忽略掉。
now
用來計(jì)算返回值的時(shí)間戳。 該參數(shù)默認(rèn)值是當(dāng)前時(shí)間time(),也可以設(shè)置為其他時(shí)間的時(shí)間戳(我一直忽略的一個(gè)功能啊,慚愧)
返回值: 成功則返回間戳,否則返回 FALSE 。在 PHP 5.1.0 之前本函數(shù)在失敗時(shí)返回 -1,后面版本返回false.
strtotime的第一個(gè)參數(shù)可以是我們常見的英文時(shí)間格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以參數(shù)now為基準(zhǔn)的時(shí)間描述,比如“+1 day”等等。
下面是后一種方式的可使用參數(shù)清單,其中“當(dāng)前時(shí)間”是指strtotime第二個(gè)參數(shù)now的值,默認(rèn)為當(dāng)前時(shí)間
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.時(shí)間參數(shù)和祥細(xì)描述:
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, “l(fā)ast month” 月,比如“l(fā)ast month”代表上一月
fortnight : two weeks; for example, “a fortnight ago” 兩周,比如“a fortnight ago”代表兩周前
week : one week 周
day: a day 天
hour: an hour 小時(shí)
minute : a minute 分鐘
min : same as minute 同“minute”
second : a second 秒
sec : same as second 同“second”
3.相關(guān)和順序說明:
+n/-n :以當(dāng)前時(shí)間算,加個(gè)減指定的時(shí)間,比如”+1 hour”是指當(dāng)前時(shí)間加一小時(shí)
ago :time relative to now; such as “24 hours ago” 以當(dāng)前時(shí)間往前算,比如”24 hours ago”代表“24小時(shí)前”
tomorrow : 24 hours later than the current date and time 以當(dāng)前時(shí)間(包括日期和時(shí)間)為標(biāo)準(zhǔn),明天同一時(shí)間
yesterday : 24 hours earlier than the current date and time 以當(dāng)前時(shí)間(包括日期和時(shí)間)為標(biāo)準(zhǔn),昨天同一時(shí)間
today : the current date and time 當(dāng)前時(shí)間(包括日期和時(shí)間)
now : the current date and time 當(dāng)前時(shí)間(包括日期和時(shí)間)
last : modifier meaning “the preceding”; for example, “l(fā)ast tuesday” 代表“上一個(gè)”,比如“l(fā)ast tuesday”代表“上周二同一時(shí)間”
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 當(dāng)天的指定時(shí)間或下面一個(gè)時(shí)間段的時(shí)間戳,比如“this 7am”給出當(dāng)天7:00的時(shí)間戳,而“this week”給出的是從當(dāng)前時(shí)間開始的一整周的時(shí)間戳,也就是當(dāng)前時(shí)間(經(jīng)本人測試:strtotime('this week')=strtotime('now'));
next : modifier meaning the current time value of the subject plus one; for example, “next hour” 當(dāng)前時(shí)間加上指定的時(shí)間,比如“next hour”是指當(dāng)前時(shí)間加上一小時(shí),即加3600
//先到這,下面的還沒時(shí)間翻譯
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.時(shí)區(qū)描述:
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中有個(gè)叫做strtotime的函數(shù)。strtotime 實(shí)現(xiàn)功能:獲取某個(gè)日期的時(shí)間戳,或獲取某個(gè)時(shí)間的時(shí)間戳。strtotime 將任何英文文本的日期時(shí)間描述解析為Unix時(shí)間戳[將系統(tǒng)時(shí)間轉(zhuǎn)化成unix時(shí)間戳]
一,獲取指定日期的unix時(shí)間戳
strtotime("2009-1-22") 示例如下:
1.echo strtotime("2009-1-22")
結(jié)果:1232553600
說明:返回2009年1月22日0點(diǎn)0分0秒時(shí)間戳
二,獲取英文文本日期時(shí)間
示例如下:
便于比較,使用date將當(dāng)時(shí)間戳與指定時(shí)間戳轉(zhuǎn)換成系統(tǒng)時(shí)間
(1)打印明天此時(shí)的時(shí)間戳strtotime("+1 day")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
結(jié)果:2009-01-23 09:40:25
(2)打印昨天此時(shí)的時(shí)間戳strtotime("-1 day")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("-1 day"))
結(jié)果:2009-01-21 09:40:25
(3)打印下個(gè)星期此時(shí)的時(shí)間戳strtotime("+1 week")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("+1 week"))
結(jié)果:2009-01-29 09:40:25
(4)打印上個(gè)星期此時(shí)的時(shí)間戳strtotime("-1 week")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("-1 week"))
結(jié)果:2009-01-15 09:40:25
(5)打印指定下星期幾的時(shí)間戳strtotime("next Thursday")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("next Thursday"))
結(jié)果:2009-01-29 00:00:00
(6)打印指定上星期幾的時(shí)間戳strtotime("last Thursday")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("last Thursday"))
結(jié)果:2009-01-15 00:00:00
以上示例可知,strtotime能將任何英文文本的日期時(shí)間描述解析為Unix時(shí)間戳,我們結(jié)合mktime()或date()格式化日期時(shí)間獲取指定的時(shí)間戳,實(shí)現(xiàn)所需要的日期時(shí)間。
希望通過本文的介紹后,你已經(jīng)能掌握strtotime函數(shù)用法。
strtotime — 將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳
格式:int strtotime ( string $time [, int $now ] )
本函數(shù)預(yù)期接受一個(gè)包含美國英語日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對于 now 參數(shù)給出的時(shí)間,如果沒有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間。
本函數(shù)將使用 TZ 環(huán)境變量(如果有的話)來計(jì)算時(shí)間戳。自 PHP 5.1.0 起有更容易的方法來定義時(shí)區(qū)用于所有的日期/時(shí)間函數(shù)。此過程在 date_default_timezone_get() 函數(shù)頁面中有說明。
Note : 如果給定的年份是兩位數(shù)字的格式,則其值 0-69 表示 2000-2069,70-100 表示 1970-2000。
參數(shù)
time
被解析的字符串,格式根據(jù) GNU » 日期輸入格式 的語法。在 PHP 5.0 之前,time 中不允許有毫秒數(shù),自 PHP 5.0 起可以有但是會被忽略掉。
now
用來計(jì)算返回值的時(shí)間戳。 該參數(shù)默認(rèn)值是當(dāng)前時(shí)間time(),也可以設(shè)置為其他時(shí)間的時(shí)間戳(我一直忽略的一個(gè)功能啊,慚愧)
返回值: 成功則返回間戳,否則返回 FALSE 。在 PHP 5.1.0 之前本函數(shù)在失敗時(shí)返回 -1,后面版本返回false.
strtotime的第一個(gè)參數(shù)可以是我們常見的英文時(shí)間格式,比如“2008-8-20”或“10 September 2000 ”等等。也可以是以參數(shù)now為基準(zhǔn)的時(shí)間描述,比如“+1 day”等等。
下面是后一種方式的可使用參數(shù)清單,其中“當(dāng)前時(shí)間”是指strtotime第二個(gè)參數(shù)now的值,默認(rèn)為當(dāng)前時(shí)間
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.時(shí)間參數(shù)和祥細(xì)描述:
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, “l(fā)ast month” 月,比如“l(fā)ast month”代表上一月
fortnight : two weeks; for example, “a fortnight ago” 兩周,比如“a fortnight ago”代表兩周前
week : one week 周
day: a day 天
hour: an hour 小時(shí)
minute : a minute 分鐘
min : same as minute 同“minute”
second : a second 秒
sec : same as second 同“second”
3.相關(guān)和順序說明:
+n/-n :以當(dāng)前時(shí)間算,加個(gè)減指定的時(shí)間,比如”+1 hour”是指當(dāng)前時(shí)間加一小時(shí)
ago :time relative to now; such as “24 hours ago” 以當(dāng)前時(shí)間往前算,比如”24 hours ago”代表“24小時(shí)前”
tomorrow : 24 hours later than the current date and time 以當(dāng)前時(shí)間(包括日期和時(shí)間)為標(biāo)準(zhǔn),明天同一時(shí)間
yesterday : 24 hours earlier than the current date and time 以當(dāng)前時(shí)間(包括日期和時(shí)間)為標(biāo)準(zhǔn),昨天同一時(shí)間
today : the current date and time 當(dāng)前時(shí)間(包括日期和時(shí)間)
now : the current date and time 當(dāng)前時(shí)間(包括日期和時(shí)間)
last : modifier meaning “the preceding”; for example, “l(fā)ast tuesday” 代表“上一個(gè)”,比如“l(fā)ast tuesday”代表“上周二同一時(shí)間”
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 當(dāng)天的指定時(shí)間或下面一個(gè)時(shí)間段的時(shí)間戳,比如“this 7am”給出當(dāng)天7:00的時(shí)間戳,而“this week”給出的是從當(dāng)前時(shí)間開始的一整周的時(shí)間戳,也就是當(dāng)前時(shí)間(經(jīng)本人測試:strtotime('this week')=strtotime('now'));
next : modifier meaning the current time value of the subject plus one; for example, “next hour” 當(dāng)前時(shí)間加上指定的時(shí)間,比如“next hour”是指當(dāng)前時(shí)間加上一小時(shí),即加3600
//先到這,下面的還沒時(shí)間翻譯
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.時(shí)區(qū)描述:
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中有個(gè)叫做strtotime的函數(shù)。strtotime 實(shí)現(xiàn)功能:獲取某個(gè)日期的時(shí)間戳,或獲取某個(gè)時(shí)間的時(shí)間戳。strtotime 將任何英文文本的日期時(shí)間描述解析為Unix時(shí)間戳[將系統(tǒng)時(shí)間轉(zhuǎn)化成unix時(shí)間戳]
一,獲取指定日期的unix時(shí)間戳
strtotime("2009-1-22") 示例如下:
1.echo strtotime("2009-1-22")
結(jié)果:1232553600
說明:返回2009年1月22日0點(diǎn)0分0秒時(shí)間戳
二,獲取英文文本日期時(shí)間
示例如下:
便于比較,使用date將當(dāng)時(shí)間戳與指定時(shí)間戳轉(zhuǎn)換成系統(tǒng)時(shí)間
(1)打印明天此時(shí)的時(shí)間戳strtotime("+1 day")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("+1 day"))
結(jié)果:2009-01-23 09:40:25
(2)打印昨天此時(shí)的時(shí)間戳strtotime("-1 day")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("-1 day"))
結(jié)果:2009-01-21 09:40:25
(3)打印下個(gè)星期此時(shí)的時(shí)間戳strtotime("+1 week")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("+1 week"))
結(jié)果:2009-01-29 09:40:25
(4)打印上個(gè)星期此時(shí)的時(shí)間戳strtotime("-1 week")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("-1 week"))
結(jié)果:2009-01-15 09:40:25
(5)打印指定下星期幾的時(shí)間戳strtotime("next Thursday")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("next Thursday"))
結(jié)果:2009-01-29 00:00:00
(6)打印指定上星期幾的時(shí)間戳strtotime("last Thursday")
當(dāng)前時(shí)間:
1.echo date("Y-m-d H:i:s",time())
結(jié)果:2009-01-22 09:40:25
指定時(shí)間:
1.echo date("Y-m-d H:i:s",strtotime("last Thursday"))
結(jié)果:2009-01-15 00:00:00
以上示例可知,strtotime能將任何英文文本的日期時(shí)間描述解析為Unix時(shí)間戳,我們結(jié)合mktime()或date()格式化日期時(shí)間獲取指定的時(shí)間戳,實(shí)現(xiàn)所需要的日期時(shí)間。
希望通過本文的介紹后,你已經(jīng)能掌握strtotime函數(shù)用法。
您可能感興趣的文章:
- PHP實(shí)現(xiàn)針對日期,月數(shù),天數(shù),周數(shù),小時(shí),分,秒等的加減運(yùn)算示例【基于strtotime】
- PHP使用strtotime獲取上個(gè)月、下個(gè)月、本月的日期
- PHP使用strtotime計(jì)算兩個(gè)給定日期之間天數(shù)的方法
- php使用date和strtotime函數(shù)輸出指定日期的方法
- PHP下獲取上個(gè)月、下個(gè)月、本月的日期(strtotime,date)
- php使用strtotime和date函數(shù)判斷日期是否有效代碼分享
- PHP時(shí)間戳 strtotime()使用方法和技巧
- php 深入理解strtotime函數(shù)的使用詳解
- PHP中strtotime函數(shù)使用方法詳解
- PHP正則匹配日期和時(shí)間(時(shí)間戳轉(zhuǎn)換)的實(shí)例代碼
- php 時(shí)間time與日期date之間的使用詳解及區(qū)別
- 非常全面的php日期時(shí)間運(yùn)算匯總
- PHP中時(shí)間加減函數(shù)strtotime用法分析
相關(guān)文章
PHP自帶函數(shù)給數(shù)字或字符串自動補(bǔ)齊位數(shù)
很多時(shí)候我們需要對數(shù)字進(jìn)行格式化,比如位數(shù)不足前面加0補(bǔ)足。用 php可以很輕易實(shí)現(xiàn),因?yàn)镻HP自帶了相關(guān)功能的函數(shù)。2014-07-07
關(guān)于PHP二進(jìn)制流 逐bit的低位在前算法(詳解)
本篇文章是對PHP二進(jìn)制流逐bit的低位在前算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
在mac?OS上進(jìn)行多個(gè)PHP版本之間切換的實(shí)現(xiàn)方法
不同項(xiàng)目使用php版本可能不同,需要安裝不同版本php,本文給大家介紹了在macOS上進(jìn)行多個(gè)?PHP?版本之間切換的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-10-10
PHP進(jìn)制轉(zhuǎn)換實(shí)例分析(2,8,16,36,64進(jìn)制至10進(jìn)制相互轉(zhuǎn)換)
這篇文章主要介紹了PHP進(jìn)制轉(zhuǎn)換,結(jié)合具體實(shí)例形式分析了2,8,16,36,64進(jìn)制至10進(jìn)制相互轉(zhuǎn)換實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
php啟動時(shí)候提示PHP startup的解決方法
配置好php環(huán)境后,每次開機(jī)都有警告提示說 PHP startup,解決這個(gè)問題很簡單只需要在php.ini 文件中修改 extension_dir配置就行2013-05-05

