PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析(2)_PHP教程
推薦:淺談php命令行用法這篇文章主要介紹了淺談php命令行用法的相關(guān)資料,需要的朋友可以參考下 Php是一個(gè)非常流行的web服務(wù)端腳本語言。其實(shí),php不僅僅可以在web服務(wù)器中充當(dāng)重要角色。在命令行一樣可以執(zhí)行。 本文中,筆者為各位介紹下php在命令行中的使用方法。 1、 查看php的版本、配置
官方文檔對(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í)間。
這是一個(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_FUNCTION(strtotime)
// 處理輸入,對(duì)于是否有第二個(gè)參數(shù)有沒的處理
// 調(diào)用相關(guān)函數(shù),實(shí)現(xiàn)字符串的解析和結(jié)果計(jì)算
// 返回結(jié)果
}
在輸入處理中,先識(shí)別兩個(gè)參數(shù)都存在的情況并進(jìn)行處理,如果不是此種狀態(tài),則處理第二個(gè)參數(shù)不存在的情況, 如果都沒有,則報(bào)錯(cuò),返回FALSE。
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ù)如下:
代碼如下:
'yesterday'
{
DEBUG_OUTPUT("yesterday");
TIMELIB_INIT;
TIMELIB_HAVE_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.d = -1;
TIMELIB_DEINIT;
return TIMELIB_RELATIVE;
}
這里有幾個(gè)關(guān)鍵的結(jié)構(gòu)體:
代碼如下:
typedef struct Scanner {
int fd;
uchar *lim, *str, *ptr, *cur, *tok, *pos;
unsigned int line, len;
struct timelib_error_container *errors;
struct timelib_time *time;
const timelib_tzdb *tzdb;
} Scanner;
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;
timelib_sll sse; /* Seconds since epoch */
unsigned int have_time, have_date, have_zone, have_relative, have_weeknr_day;
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;
typedef struct timelib_rel_time {
timelib_sll y, m, d; /* Years, Months and Days */
timelib_sll h, i, s; /* Hours, mInutes and Seconds */
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 */
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 */
timelib_special special;
unsigned int have_weekday_relative, have_special_relative;
} timelib_rel_time;
s->time->relative.d = -1;所表示的意思是當(dāng)前時(shí)間的相對(duì)天數(shù)是-1。 這只是中間詞法解析的中間結(jié)果,但是最后結(jié)果是通過這些中間結(jié)果計(jì)算出來的。
strtotime(“-1 month”)求值失敗的原因
雖然strtotime(“-1 month”)這種方法對(duì)于后一個(gè)月比前一個(gè)月的天數(shù)的情況會(huì)求值失敗,但是從其本質(zhì)上來說,這并沒有錯(cuò)。 PHP這樣實(shí)現(xiàn)也無可厚非。只是我們的需求決定了我們不能使用這種方法,因此我們稱其為求值失敗。
我們來看它的實(shí)現(xiàn)過程,由于沒有第二個(gè)參數(shù),所以程序使用默認(rèn)的當(dāng)前時(shí)間。 第一個(gè)參數(shù)傳入的是-1 month字符串,這個(gè)字符串所對(duì)應(yīng)的re文件中的正則為:
代碼如下:
reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year') 's'?) | 'weeks' | daytext;
relnumber = ([+-]*[ \t]*[0-9]+);
relative = relnumber space? (reltextunit | 'week' );
最終relative會(huì)對(duì)應(yīng)一系列操作,程序會(huì)識(shí)別出前面的-1 和后面的month字符串,month對(duì)應(yīng)一種操作類型:TIMELIB_MONTH。 在此之后,根據(jù)識(shí)別出來的數(shù)字和操作類型執(zhí)行操作,如下代碼:
代碼如下:
case TIMELIB_MONTH: s->time->relative.m += amount * relunit->multiplier; break;
如上代碼,則是直接記錄月份的相對(duì)值減一。 但是對(duì)于類似于3月31號(hào)這樣的情況,2月沒有31號(hào),程序會(huì)自動(dòng)將日期計(jì)算到下一個(gè)月。
分享:php curl登陸qq后獲取用戶信息時(shí)證書錯(cuò)誤這篇文章主要介紹了php curl登陸qq后獲取用戶信息時(shí)證書錯(cuò)誤,需要的朋友可以參考下 今晚開放ecmall商城的QQ登陸功能,在回調(diào)時(shí)產(chǎn)生錯(cuò)誤,file_get_contents函數(shù)執(zhí)行時(shí),沒有抓取到正確的信息,于是改用curl,但是提示證書錯(cuò)誤。 在網(wǎng)上找到了解決方法,就是去掉證書認(rèn)
- 淺談php命令行用法
- php curl登陸qq后獲取用戶信息時(shí)證書錯(cuò)誤
- ecshop實(shí)現(xiàn)smtp發(fā)送郵件
- curl 模擬登錄 實(shí)現(xiàn)教程
- php生成Excel文件 實(shí)現(xiàn)代碼
- PHP-redis命令文檔
- PHP變量引用(&)、函數(shù)引用和對(duì)象引用
- CI中view的重寫 代碼
- 如何在PHP語言中使用JSON
- PHP 轉(zhuǎn)義正則表達(dá)式字符: preg_quote
- PHP中的一些MySQL函數(shù)
- PHP提示:Fatal error: Allowed memory size of
- 相關(guān)鏈接:
復(fù)制本頁鏈接| 搜索PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析(2)
- 教程說明:
PHP教程-PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析(2)
。