《PHP教程:PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析》要點(diǎn):
本文介紹了PHP教程:PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
源碼位置:\ext\date\php_date.c
PHP教程
??? timelib_time *t, *now;
??? timelib_tzinfo *tzi;PHP教程
??? tzi = get_timezone_info(TSRMLS_C);PHP教程
??? if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, “sl”, ×, &time_len, &preset_ts) != FAILURE) {
??????? /* We have an initial timestamp */
??????? now = timelib_time_ctor();PHP教程
??????? initial_ts = emalloc(25);
??????? snprintf(initial_ts, 24, “@%ld UTC”, preset_ts);
??????? t = timelib_strtotime(initial_ts, strlen(initial_ts), NULL, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); /* we ignore the error here, as this should never fail */
??????? timelib_update_ts(t, tzi);
??????? now->tz_info = tzi;
??????? now->zone_type = TIMELIB_ZONETYPE_ID;
??????? timelib_unixtime2local(now, t->sse);
??????? timelib_time_dtor(t);
??????? efree(initial_ts);
??? } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “s|l”, ×, &time_len, &preset_ts) != FAILURE) {
??????? /* We have no initial timestamp */
??????? now = timelib_time_ctor();
??????? now->tz_info = tzi;
??????? now->zone_type = TIMELIB_ZONETYPE_ID;
??????? timelib_unixtime2local(now, (timelib_sll) time(NULL));
??? } else {
??????? RETURN_FALSE;
??? }PHP教程
??? if (!time_len) {
??????? timelib_time_dtor(now);???
??????? RETURN_FALSE;
??? }PHP教程
??? t = timelib_strtotime(times, time_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
??? error1 = error->error_count;
??? timelib_error_container_dtor(error);
??? timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
??? timelib_update_ts(t, tzi);
??? ts = timelib_date_to_int(t, &error2);PHP教程
??? timelib_time_dtor(now);
??? timelib_time_dtor(t);PHP教程
??? if (error1 || error2) {
??????? RETURN_FALSE;
??? } else {
??????? RETURN_LONG(ts);
??? }
}
/* }}} */
PHP教程
因此也引出寫這篇文章,本文包含如下內(nèi)容:PHP教程
1).strtotime函數(shù)的一些用法
2).strtotime函數(shù)的實(shí)現(xiàn)基來源根基理
3).strtotime(“-1 month”)求值失敗的原因PHP教程
strtotime函數(shù)的一些用法PHP教程
1、 strtotime(“JAN”)和strtotime(“January”)PHP教程
這兩個(gè)用法的效果是一樣的,都是返回指定月份的本日,如果指定月份沒有本日,則順延到下一個(gè)月. 如在2011-03-31計(jì)算二月,代碼:
PHP教程
2、 first關(guān)鍵字PHP教程
first是一個(gè)輔助型的關(guān)鍵字,它可以與星期,天等可以指定確認(rèn)值的關(guān)鍵字組合使用,如求2011年的第一個(gè)星期天:
PHP教程
與first類似,previous關(guān)鍵字可以與星期,天組合使用,表示指定時(shí)間的前一個(gè)星期幾或前一天.如下所示代碼:
PHP教程
next關(guān)鍵字與previous相反,它表示下一個(gè)星期幾或后一天.PHP教程
4、 last關(guān)鍵字PHP教程
last關(guān)鍵字既可以作為上一個(gè),也可以作為最后一個(gè).如求上一個(gè)星期天的日期:
PHP教程
當(dāng)程序作為最后時(shí),其應(yīng)用場(chǎng)景是指定日期所在月的最后一天,相當(dāng)于date(“t”)的結(jié)果.如求2000年2月的最后一天:
PHP教程
5、 back和front關(guān)鍵字PHP教程
這兩個(gè)關(guān)鍵字是對(duì)一天中的小時(shí)的向前和向后操作,其調(diào)用格式如下:
PHP教程
strtotime函數(shù)的實(shí)現(xiàn)基本原理
官方文檔對(duì)于strtotime函數(shù)的說明是這樣的:本函數(shù)預(yù)期接受一個(gè)包括美國(guó)英語日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳 (自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對(duì)于 now 參數(shù)給出的時(shí)間,如果沒有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間.PHP教程
這是一個(gè)標(biāo)準(zhǔn)PHP內(nèi)置函數(shù),從PHP4起就已經(jīng)存在.strtotime函數(shù)是以一個(gè)擴(kuò)展的方式加載進(jìn)來的,在ext/date目錄下有其全部實(shí)現(xiàn). 作為一個(gè)標(biāo)準(zhǔn)的內(nèi)置函數(shù),其定義格式也是標(biāo)準(zhǔn)的,如下:
PHP教程
??? //? 調(diào)用相關(guān)函數(shù),實(shí)現(xiàn)字符串的解析和結(jié)果計(jì)算PHP教程
??? //? 返回結(jié)果
}
PHP教程
strtotime函數(shù)的第一個(gè)參數(shù)是一個(gè)字符串,對(duì)于這個(gè)字符串,由于其復(fù)雜性,PHP使用了其詞法解析一樣的工具:re2c.在/ext/date/lib目錄下,從parse_date.re文件我們可以看到其原始的re文件. 當(dāng)用戶以參數(shù)的形式傳入一個(gè)字符串,此字符串將交給此程序處理,針對(duì)其字符串的不同,匹配不同的處理函數(shù). 如strtotime(“yesterday”)調(diào)用,分析字符串時(shí),將匹配yesterday字符串,此字符串對(duì)應(yīng)函數(shù)如下:
PHP教程
??? s->time->relative.d = -1;
??? TIMELIB_DEINIT;
??? return TIMELIB_RELATIVE;
}
PHP教程
??? struct timelib_time *time;
??? const timelib_tzdb? *tzdb;
} Scanner;PHP教程
typedef struct timelib_time {
??? timelib_sll????? y, m, d;???? /* Year, Month, Day */
??? timelib_sll????? h, i, s;???? /* Hour, mInute, Second */
??? double?????????? f;?????????? /* Fraction */
??? int????????????? z;?????????? /* GMT offset in minutes */
??? char??????????? *tz_abbr;???? /* Timezone abbreviation (display only) */
??? timelib_tzinfo? *tz_info;???? /* Timezone structure */
??? signed int?????? dst;???????? /* Flag if we were parsing a DST zone */
??? timelib_rel_time relative;PHP教程
??? timelib_sll????? sse;???????? /* Seconds since epoch */PHP教程
??? unsigned int?? have_time, have_date, have_zone, have_relative, have_weeknr_day;PHP教程
??? unsigned int?? sse_uptodate; /* !0 if the sse member is up to date with the date/time members */
??? unsigned int?? tim_uptodate; /* !0 if the date/time members are up to date with the sse member */
??? unsigned int?? is_localtime; /*? 1 if the current struct represents localtime, 0 if it is in GMT */
??? unsigned int?? zone_type;??? /*? 1 time offset,
????????????????????????????????? *? 3 TimeZone identifier,
????????????????????????????????? *? 2 TimeZone abbreviation */
} timelib_time;PHP教程
typedef struct timelib_rel_time {
??? timelib_sll y, m, d; /* Years, Months and Days */
??? timelib_sll h, i, s; /* Hours, mInutes and Seconds */PHP教程
??? int weekday; /* Stores the day in 'next monday' */
??? int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */PHP教程
??? int first_last_day_of;
??? int invert; /* Whether the difference should be inverted */
??? timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */PHP教程
??? timelib_special? special;
??? unsigned int?? have_weekday_relative, have_special_relative;
} timelib_rel_time;
PHP教程
strtotime(“-1 month”)求值失敗的原因PHP教程
雖然strtotime(“-1 month”)這種辦法對(duì)于后一個(gè)月比前一個(gè)月的天數(shù)的情況會(huì)求值失敗,但是從其本質(zhì)上來說,這并沒有錯(cuò). PHP這樣實(shí)現(xiàn)也無可厚非.只是我們的需求決定了我們不能使用這種辦法,因此我們稱其為求值失敗.PHP教程
我們來看它的實(shí)現(xiàn)過程,由于沒有第二個(gè)參數(shù),所以程序使用默認(rèn)的當(dāng)前時(shí)間. 第一個(gè)參數(shù)傳入的是-1 month字符串,這個(gè)字符串所對(duì)應(yīng)的re文件中的正則為:
PHP教程
relnumber = ([+-]*[ \t]*[0-9]+);
relative = relnumber space? (reltextunit | 'week' );
PHP教程
歡迎參與《PHP教程:PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/12465.html