国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

PHP轉(zhuǎn)Unix時(shí)間戳strtotime函數(shù)源碼分析及使用方法

2024-09-04 11:45:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

PHP的strtotime() 函數(shù)將任何英文文本的日期時(shí)間描述解析為 Unix 時(shí)間戳,現(xiàn)在我們來(lái)講講strtotime函數(shù)源碼分析及使用方法.

源碼位置:extdatephp_date.c,代碼如下:

  1. /* {{{ proto int strtotime(string time [, int now ]) 
  2.    Convert string representation of date and time to a timestamp */ 
  3. PHP_FUNCTION(strtotime
  4.     char *times, *initial_ts; 
  5.     int   time_len, error1, error2; 
  6.     struct timelib_error_container *error; 
  7.     long  preset_ts = 0, ts; 
  8.  
  9.     timelib_time *t, *now; 
  10.     timelib_tzinfo *tzi; 
  11.  
  12.     tzi = get_timezone_info(TSRMLS_C); 
  13.  
  14.     if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, “sl”, &times, &time_len, &preset_ts) != FAILURE) { 
  15.         /* We have an initial timestamp */ 
  16.         now = timelib_time_ctor(); 
  17.  
  18.         initial_ts = emalloc(25); 
  19.         snprintf(initial_ts, 24, “@%ld UTC”, preset_ts); 
  20.         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 */ 
  21.         timelib_update_ts(t, tzi); 
  22.         now->tz_info = tzi; 
  23.         now->zone_type = TIMELIB_ZONETYPE_ID; 
  24.         timelib_unixtime2local(now, t->sse); 
  25.         timelib_time_dtor(t); 
  26.         efree(initial_ts); 
  27.     } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “s|l”, &times, &time_len, &preset_ts) != FAILURE) { 
  28.         /* We have no initial timestamp */ 
  29.         now = timelib_time_ctor(); 
  30.         now->tz_info = tzi; 
  31.         now->zone_type = TIMELIB_ZONETYPE_ID; 
  32.         timelib_unixtime2local(now, (timelib_sll) time(NULL)); 
  33.     } else { 
  34.         RETURN_FALSE; 
  35.     } 
  36.  
  37.     if (!time_len) { 
  38.         timelib_time_dtor(now);    
  39.         RETURN_FALSE; 
  40.     } 
  41.  
  42.     t = timelib_strtotime(times, time_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); 
  43.     error1 = error->error_count; 
  44.     timelib_error_container_dtor(error); 
  45.     timelib_fill_holes(t, now, TIMELIB_NO_CLONE); 
  46.     timelib_update_ts(t, tzi); 
  47.     ts = timelib_date_to_int(t, &error2); 
  48.  
  49.     timelib_time_dtor(now); 
  50.     timelib_time_dtor(t); 
  51.  
  52.     if (error1 || error2) { 
  53.         RETURN_FALSE; 
  54.     } else { 
  55.         RETURN_LONG(ts); 
  56.     } 
  57. /* }}} */ 

strtotime函數(shù)在使用strtotime(“-1 month”)求上一個(gè)月的今天時(shí)會(huì)出一些狀況,因此也引出寫這篇文章,本文包括如下內(nèi)容:

strtotime函數(shù)的一些用法,strtotime函數(shù)的實(shí)現(xiàn)基本原理,strtotime(“-1 month”)求值失敗的原因.

strtotime函數(shù)的一些用法

1、strtotime(“JAN”)和strtotime(“January”)

這兩個(gè)用法的效果是一樣的,都是返回指定月份的今天,如果指定月份沒(méi)有今天,則順延到下一個(gè)月,如在2011-03-31計(jì)算二月,代碼:

echo date("Y-m-d H:i:s", strtotime("feb", strtotime("2011-03-31")));

程序會(huì)輸出: 2011-03-03 00:00:00,從表象來(lái)看,這個(gè)結(jié)果也許不一定是我們想要的,但是這也算是一種解決方案,這種方案是由什么決定的呢?strtotime函數(shù)在執(zhí)行月份的計(jì)算時(shí)只計(jì)算了月份,相當(dāng)于直接將月份設(shè)置為指定的月份的值,而如jan,january都會(huì)有一個(gè)對(duì)應(yīng)內(nèi)部數(shù)值.

2、first關(guān)鍵字

first是一個(gè)輔助型的關(guān)鍵字,它可以與星期,天等可以指定確認(rèn)值的關(guān)鍵字組合使用,如求2011年的第一個(gè)星期天:

echo date("Y-m-d H:i:s", strtotime("second sunday", strtotime("2011-01-01"))), "<br />";

在PHP的源碼中,對(duì)于first與星期和天的組合使用是分開(kāi)的,即first day對(duì)應(yīng)一個(gè)處理操作,在最終的C實(shí)現(xiàn)中,天的值指定為1,即time結(jié)構(gòu)中的d字段指定為1,如下代碼:

  1. switch (time->relative.first_last_day_of) { 
  2.     case 1: /* first */ 
  3.         time->d = 1; 
  4.         break
  5.     case 2: /* last */ 
  6.         time->d = 0; 
  7.         time->m++; 
  8.         break

3、previous和next關(guān)鍵字

與first類似,previous關(guān)鍵字可以與星期,天組合使用,表示指定時(shí)間的前一個(gè)星期幾或前一天,如下所示代碼:

echo date("Y-m-d H:i:s", strtotime("previous sunday", strtotime("2011-02-01"))), "<br />";

程序會(huì)輸出:2011-01-30 00:00:00

程序求2011-02-01的前一個(gè)星期天.

next關(guān)鍵字與previous相反,它表示下一個(gè)星期幾或后一天.

4、last關(guān)鍵字

last關(guān)鍵字既可以作為上一個(gè),也可以作為最后一個(gè),如求上一個(gè)星期天的日期:

echo date("Y-m-d H:i:s", strtotime("last sunday", strtotime("2011-02-05"))), "<br />";

程序會(huì)輸出:2011-01-30 00:00:00

當(dāng)程序作為最后時(shí),其應(yīng)用場(chǎng)景是指定日期所在月的最后一天,相當(dāng)于date(“t”)的結(jié)果,如求2000年2月的最后一天:

echo date("Y-m-d H:i:s", strtotime("last day", strtotime("2000-02-01"))), "<br />";

first、previous、last和this關(guān)鍵字在re文件中屬于同一組.

5、back和front關(guān)鍵字

這兩個(gè)關(guān)鍵字是對(duì)一天中的小時(shí)的向前和向后操作,其調(diào)用格式如下:

  1. echo date("Y-m-d H:i:s"strtotime("back of 24"strtotime("2011-02-01"))), "<br />"
  2. echo date("Y-m-d H:i:s"strtotime("front of 24"strtotime("2011-02-01"))), "<br />"

back表示將時(shí)間設(shè)置指定小時(shí)值的后一個(gè)小時(shí)的15分的位置,如果是24點(diǎn),則算到第二天的0點(diǎn)15分.

front表示將時(shí)間設(shè)置指定小時(shí)值的前一個(gè)小時(shí)的45分的位置,如果是0點(diǎn),則算前一天的23點(diǎn)45分.

上面的代碼輸出:2011-02-02 00:15:00 2011-02-01 23:45:00,其中back of和front of后接的數(shù)組必須大于等于0并且小于等于24.

strtotime函數(shù)的實(shí)現(xiàn)基本原理

官方文檔對(duì)于strtotime函數(shù)的說(shuō)明是這樣的:本函數(shù)預(yù)期接受一個(gè)包含美國(guó)英語(yǔ)日期格式的字符串并嘗試將其解析為 Unix 時(shí)間戳(自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對(duì)于 now 參數(shù)給出的時(shí)間,如果沒(méi)有提供此參數(shù)則用系統(tǒng)當(dāng)前時(shí)間.

這是一個(gè)標(biāo)準(zhǔn)PHP內(nèi)置函數(shù),從PHP4起就已經(jīng)存在,strtotime函數(shù)是以一個(gè)擴(kuò)展的方式加載進(jìn)來(lái)的,在ext/date目錄下有其全部實(shí)現(xiàn),作為一個(gè)標(biāo)準(zhǔn)的內(nèi)置函數(shù),其定義格式也是標(biāo)準(zhǔn)的,如下:

  1. PHP_FUNCTION(strtotime
  2.     //  處理輸入,對(duì)于是否有第二個(gè)參數(shù)有沒(méi)的處理 
  3.  
  4.     //  調(diào)用相關(guān)函數(shù),實(shí)現(xiàn)字符串的解析和結(jié)果計(jì)算 
  5.  
  6.     //  返回結(jié)果 

在輸入處理中,先識(shí)別兩個(gè)參數(shù)都存在的情況并進(jìn)行處理,如果不是此種狀態(tài),則處理第二個(gè)參數(shù)不存在的情況,如果都沒(méi)有,則報(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ù)如下:

  1. 'yesterday' 
  2.     DEBUG_OUTPUT("yesterday"); 
  3.     TIMELIB_INIT; 
  4.     TIMELIB_HAVE_RELATIVE(); 
  5.     TIMELIB_UNHAVE_TIME(); 
  6.  
  7.     s->time->relative.d = -1; 
  8.     TIMELIB_DEINIT; 
  9.     return TIMELIB_RELATIVE; 

這里有幾個(gè)關(guān)鍵的結(jié)構(gòu)體:

  1. typedef struct Scanner { 
  2.     int           fd; 
  3.     uchar        *lim, *str, *ptr, *cur, *tok, *pos; 
  4.     unsigned int  line, len; 
  5.     struct timelib_error_container *errors; 
  6.  
  7.     struct timelib_time *time; 
  8.     const timelib_tzdb  *tzdb; 
  9. } Scanner; 
  10.  
  11. typedef struct timelib_time { 
  12.     timelib_sll      y, m, d;     /* Year, Month, Day */ 
  13.     timelib_sll      h, i, s;     /* Hour, mInute, Second */ 
  14.     double           f;           /* Fraction */ 
  15.     int              z;           /* GMT offset in minutes */ 
  16.     char            *tz_abbr;     /* Timezone abbreviation (display only) */ 
  17.     timelib_tzinfo  *tz_info;     /* Timezone structure */ 
  18.     signed int       dst;         /* Flag if we were parsing a DST zone */ 
  19.     timelib_rel_time relative; 
  20.  
  21.     timelib_sll      sse;         /* Seconds since epoch */ 
  22.  
  23.     unsigned int   have_time, have_date, have_zone, have_relative, have_weeknr_day; 
  24.  
  25.     unsigned int   sse_uptodate; /* !0 if the sse member is up to date with the date/time members */ 
  26.     unsigned int   tim_uptodate; /* !0 if the date/time members are up to date with the sse member */ 
  27.     unsigned int   is_localtime; /*  1 if the current struct represents localtime, 0 if it is in GMT */ 
  28.     unsigned int   zone_type;    /*  1 time offset, 
  29.                                   *  3 TimeZone identifier, 
  30.                                   *  2 TimeZone abbreviation */ 
  31. } timelib_time; 
  32.  
  33. typedef struct timelib_rel_time { 
  34.     timelib_sll y, m, d; /* Years, Months and Days */ 
  35.     timelib_sll h, i, s; /* Hours, mInutes and Seconds */ 
  36.     //開(kāi)源軟件:Vevb.com 
  37.     int weekday; /* Stores the day in 'next monday' */ 
  38.     int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */ 
  39.  
  40.     int first_last_day_of; 
  41.     int invert; /* Whether the difference should be inverted */ 
  42.     timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */ 
  43.  
  44.     timelib_special  special; 
  45.     unsigned int   have_weekday_relative, have_special_relative; 
  46. } timelib_rel_time; 

s->time->relative.d = -1;所表示的意思是當(dāng)前時(shí)間的相對(duì)天數(shù)是-1,這只是中間詞法解析的中間結(jié)果,但是最后結(jié)果是通過(guò)這些中間結(jié)果計(jì)算出來(lái)的.

strtotime(“-1 month”)求值失敗的原因

雖然strtotime(“-1 month”)這種方法對(duì)于后一個(gè)月比前一個(gè)月的天數(shù)的情況會(huì)求值失敗,但是從其本質(zhì)上來(lái)說(shuō),這并沒(méi)有錯(cuò),PHP這樣實(shí)現(xiàn)也無(wú)可厚非,只是我們的需求決定了我們不能使用這種方法,因此我們稱其為求值失敗.

我們來(lái)看它的實(shí)現(xiàn)過(guò)程,由于沒(méi)有第二個(gè)參數(shù),所以程序使用默認(rèn)的當(dāng)前時(shí)間,第一個(gè)參數(shù)傳入的是-1 month字符串,這個(gè)字符串所對(duì)應(yīng)的re文件中的正則為:

  1. reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year''s'?) | 'weeks' | daytext; 
  2.  
  3. relnumber = ([+-]*[ t]*[0-9]+); 
  4. relative = relnumber space? (reltextunit | 'week' ); 

最終relative會(huì)對(duì)應(yīng)一系列操作,程序會(huì)識(shí)別出前面的-1 和后面的month字符串,month對(duì)應(yīng)一種操作類型:TIMELIB_MONTH,在此之后,根據(jù)識(shí)別出來(lái)的數(shù)字和操作類型執(zhí)行操作,如下代碼:

case TIMELIB_MONTH:s->time->relative.m += amount * relunit->multiplier; break;

如上代碼,則是直接記錄月份的相對(duì)值減一,但是對(duì)于類似于3月31號(hào)這樣的情況,2月沒(méi)有31號(hào),程序會(huì)自動(dòng)將日期計(jì)算到下一個(gè)月.

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 无极县| 苏尼特左旗| 固镇县| 广灵县| 邮箱| 岐山县| 山丹县| 丹寨县| 泗阳县| 绥棱县| 兴宁市| 巫山县| 宜川县| 吴堡县| 南岸区| 宿迁市| 曲靖市| 电白县| 龙井市| 桑植县| 卓资县| 庆元县| 酒泉市| 海安县| 齐河县| 五指山市| 海淀区| 宜宾县| 右玉县| 额济纳旗| 绥德县| 罗城| 特克斯县| 清徐县| 华池县| 焦作市| 南投市| 长丰县| 安乡县| 内乡县| 吴旗县|