最優(yōu)良人 » 日期函數(shù) http://www.dgkai.cn/blog 中山php|最優(yōu)網(wǎng)絡(luò) Mon, 13 May 2013 04:56:43 +0000 en hourly 1 http://wordpress.org/?v=3.1.4 php時(shí)間日期函數(shù)date,getdate,strtotime,strftime,strptime,time,mktime,microtime匯總對(duì)比 http://www.dgkai.cn/blog/view-145.html http://www.dgkai.cn/blog/view-145.html#comments Fri, 19 Aug 2011 08:56:51 +0000 lin http://www.dgkai.cn/blog/?p=145 date — 格式化一個(gè)本地時(shí)間/日期(把時(shí)間戳變成文本格式)
string date ( string $format [, int $timestamp ] )
timestamp 是可選的,默認(rèn)值為 time()。
$today = date("H:i:s"); // 17:16:17

getdate — 取得日期/時(shí)間信息(把時(shí)間戳的信息存到數(shù)組)
array getdate ([ int $timestamp ] )
<?php
$today = getdate();
print_r($today);
?>
返回
Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)

strtotime — 將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳 (把文本格式變成時(shí)間戳)
int strtotime ( string $time [, int $now ] )
函數(shù)預(yù)期接受一個(gè)包含美國英語日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對(duì)于 now 參數(shù)給出的時(shí)間,如果沒有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間。
<?php
echo strtotime("2011-9-9"), "\n";
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";
?>
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, "last month" 月,比如"last 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, "last tuesday" 代表"上一個(gè)",比如"last 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

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

strftime — 根據(jù)區(qū)域設(shè)置格式化本地時(shí)間/日期 (把時(shí)間戳變成文本格式)
string strftime ( string $format [, int $timestamp ] )
strftime()工作的方式和date()沒有什么不同,除了特殊格式化字符的前面必須添加一個(gè)百分號(hào)%。
strftime()有兩個(gè)好處。第一個(gè)好處是如果你使用setlocale()函數(shù),你可以通過strftime得到相應(yīng)語言的月份的名稱。另外的一個(gè)好處是你可以將特別的日期和時(shí)間的格式化字符包含在你的字符串中。這同時(shí)也意味著無論你是否要學(xué)習(xí)date()函數(shù)的所有特殊格式化字符,你都必須學(xué)習(xí)一整套完全不同的格式化字符。
一般phper用date會(huì)比較順手吧。

strptime — 解析由 strftime() 生成的日期/時(shí)間 (把文本格式的日期信息存到數(shù)組)
array strptime ( string $date , string $format )
類似于date函數(shù)的getdate
<?php
$format = '%d/%m/%Y %H:%M:%S';
$strf = strftime($format);

echo "$strf\n";

print_r(strptime($strf, $format));
?>
輸出
03/10/2004 15:54:19

Array
(
[tm_sec] => 19
[tm_min] => 54
[tm_hour] => 15
[tm_mday] => 3
[tm_mon] => 9
[tm_year] => 104
[tm_wday] => 0
[tm_yday] => 276
[unparsed] =>
)

time — 返回當(dāng)前的 Unix 時(shí)間戳
int time ( void )

mktime — 取得一個(gè)日期的 Unix 時(shí)間戳
int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]]] )
參數(shù)可以從右向左省略,任何省略的參數(shù)會(huì)被設(shè)置成本地日期和時(shí)間的當(dāng)前值,參數(shù)全部為空則與time()相同

microtime — 返回當(dāng)前 Unix 時(shí)間戳和微秒數(shù)
microtime() 當(dāng)前 Unix 時(shí)間戳以及微秒數(shù)。本函數(shù)僅在支持 gettimeofday() 系統(tǒng)調(diào)用的操作系統(tǒng)下可用。
如果調(diào)用時(shí)不帶可選參數(shù),本函數(shù)以 "msec sec" 的格式返回一個(gè)字符串,其中 sec 是自 Unix 紀(jì)元(0:00:00 January 1, 1970 GMT)起到現(xiàn)在的秒數(shù),msec 是微秒部分。字符串的兩部分都是以秒為單位返回的。
如果給出了 get_as_float 參數(shù)并且其值等價(jià)于 TRUE,microtime() 將返回一個(gè)浮點(diǎn)數(shù)。默認(rèn)為false
<?php
echo microtime(true);//1313743963.77
echo microtime(false);//0.10190200 1313744036
?>

]]>
http://www.dgkai.cn/blog/view-145.html/feed 379
欧美激情第1页| 国产精品久久777777| 五月婷丁香| zz00zzz00zz0o另类| 五月天丁香视频| 超碰人人网| 一本大道综合久久精品| 欧美劲爆第30页| 国产破处视频| 个旧市| 久久成人网| 国产午夜免费在线观看| 欧美日韩中文国产一区发布| 成人性生交大片免高清| 性感少妇涩av| 懂色av一区二区三区| 五月婷婷久久一区综合国产_| www.色播.com| 日韩无码精品黄片| 久久a久久精品综合| 国产乱码欧美亚洲一区二区三区| 人人干熟女| 色综合小说网| 国产乱人无码AV在线A| 成人亚洲性情网站www在线观看| 亚洲欧美区精品| 夫妇交换性三中文字幕| heyzo6778| 91啦丨国产丨蚪窝人妻首页| 亚洲欧美日韩 综合| 成年人免费观看爱爱| 狠狠躁夜夜躁人爽| 同性恋网站| 六月色综合久| 欧美黄片在线看| 国内精自视频品线二三区| 久久精品国产清自在天天线| 久久久A级| www.欧美一级.com| 婷婷激情五月天| 婷婷色网站|