PHP strtotime函數用法、實現原理和源碼分析(2)_PHP教程
推薦:淺談php命令行用法這篇文章主要介紹了淺談php命令行用法的相關資料,需要的朋友可以參考下 Php是一個非常流行的web服務端腳本語言。其實,php不僅僅可以在web服務器中充當重要角色。在命令行一樣可以執行。 本文中,筆者為各位介紹下php在命令行中的使用方法。 1、 查看php的版本、配置
官方文檔對于strtotime函數的說明是這樣的:本函數預期接受一個包含美國英語日期格式的字符串并嘗試將其解析為 Unix 時間戳 (自 January 1 1970 00:00:00 GMT 起的秒數),其值相對于 now 參數給出的時間,如果沒有提供此參數則用系統當前時間。
這是一個標準PHP內置函數,從PHP4起就已經存在。strtotime函數是以一個擴展的方式加載進來的,在ext/date目錄下有其全部實現。 作為一個標準的內置函數,其定義格式也是標準的,如下:
代碼如下:
PHP_FUNCTION(strtotime)
// 處理輸入,對于是否有第二個參數有沒的處理
// 調用相關函數,實現字符串的解析和結果計算
// 返回結果
}
在輸入處理中,先識別兩個參數都存在的情況并進行處理,如果不是此種狀態,則處理第二個參數不存在的情況, 如果都沒有,則報錯,返回FALSE。
strtotime函數的第一個參數是一個字符串,對于這個字符串,由于其復雜性,PHP使用了其詞法解析一樣的工具:re2c。在/ext/date/lib目錄下,從parse_date.re文件我們可以看到其原始的re文件。 當用戶以參數的形式傳入一個字符串,此字符串將交給此程序處理,針對其字符串的不同,匹配不同的處理函數。 如strtotime(“yesterday”)調用,分析字符串時,將匹配yesterday字符串,此字符串對應函數如下:
代碼如下:
'yesterday'
{
DEBUG_OUTPUT("yesterday");
TIMELIB_INIT;
TIMELIB_HAVE_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.d = -1;
TIMELIB_DEINIT;
return TIMELIB_RELATIVE;
}
這里有幾個關鍵的結構體:
代碼如下:
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;所表示的意思是當前時間的相對天數是-1。 這只是中間詞法解析的中間結果,但是最后結果是通過這些中間結果計算出來的。
strtotime(“-1 month”)求值失敗的原因
雖然strtotime(“-1 month”)這種方法對于后一個月比前一個月的天數的情況會求值失敗,但是從其本質上來說,這并沒有錯。 PHP這樣實現也無可厚非。只是我們的需求決定了我們不能使用這種方法,因此我們稱其為求值失敗。
我們來看它的實現過程,由于沒有第二個參數,所以程序使用默認的當前時間。 第一個參數傳入的是-1 month字符串,這個字符串所對應的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會對應一系列操作,程序會識別出前面的-1 和后面的month字符串,month對應一種操作類型:TIMELIB_MONTH。 在此之后,根據識別出來的數字和操作類型執行操作,如下代碼:
代碼如下:
case TIMELIB_MONTH: s->time->relative.m += amount * relunit->multiplier; break;
如上代碼,則是直接記錄月份的相對值減一。 但是對于類似于3月31號這樣的情況,2月沒有31號,程序會自動將日期計算到下一個月。
分享:php curl登陸qq后獲取用戶信息時證書錯誤這篇文章主要介紹了php curl登陸qq后獲取用戶信息時證書錯誤,需要的朋友可以參考下 今晚開放ecmall商城的QQ登陸功能,在回調時產生錯誤,file_get_contents函數執行時,沒有抓取到正確的信息,于是改用curl,但是提示證書錯誤。 在網上找到了解決方法,就是去掉證書認
- 相關鏈接:
- 教程說明:
PHP教程-PHP strtotime函數用法、實現原理和源碼分析(2)
。