最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP strtotime函數(shù)用法、實現(xiàn)原理和源碼分析

 更新時間:2015年02月04日 10:19:15   投稿:junjie  
這篇文章主要介紹了PHP strtotime函數(shù)用法、實現(xiàn)原理和源碼分析,本文講解了strtotime函數(shù)的一些用法、strtotime函數(shù)的實現(xiàn)基本原理、strtotime(“-1 month”)求值失敗的原因等內(nèi)容,需要的朋友可以參考下

源碼位置:\ext\date\php_date.c

復(fù)制代碼 代碼如下:

/* {{{ proto int strtotime(string time [, int now ])
   Convert string representation of date and time to a timestamp */
PHP_FUNCTION(strtotime)
{
    char *times, *initial_ts;
    int   time_len, error1, error2;
    struct timelib_error_container *error;
    long  preset_ts = 0, ts;

    timelib_time *t, *now;
    timelib_tzinfo *tzi;

    tzi = get_timezone_info(TSRMLS_C);

    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, “sl”, &times, &time_len, &preset_ts) != FAILURE) {
        /* We have an initial timestamp */
        now = timelib_time_ctor();

        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”, &times, &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;
    }

    if (!time_len) {
        timelib_time_dtor(now);   
        RETURN_FALSE;
    }

    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);

    timelib_time_dtor(now);
    timelib_time_dtor(t);

    if (error1 || error2) {
        RETURN_FALSE;
    } else {
        RETURN_LONG(ts);
    }
}
/* }}} */


strtotime函數(shù)在使用strtotime(“-1 month”)求上一個月的今天時會出一些狀況,

因此也引出寫這篇文章,本文包括如下內(nèi)容:

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

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

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

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

復(fù)制代碼 代碼如下:

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

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

2、 first關(guān)鍵字

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

復(fù)制代碼 代碼如下:

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

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

switch (time->relative.first_last_day_of) {
    case 1: /* first */
        time->d = 1;
        break;
    case 2: /* last */
        time->d = 0;
        time->m++;
        break;
}

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

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

復(fù)制代碼 代碼如下:

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

程序會輸出:2011-01-30 00:00:00
程序求2011-02-01的前一個星期天。

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

4、 last關(guān)鍵字

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

復(fù)制代碼 代碼如下:

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

程序會輸出: 2011-01-30 00:00:00

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

復(fù)制代碼 代碼如下:

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)鍵字

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

復(fù)制代碼 代碼如下:

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

back表示將時間設(shè)置指定小時值的后一個小時的15分的位置。如果是24點,則算到第二天的0點15分。
front表示將時間設(shè)置指定小時值的前一個小時的45分的位置。如果是0點,則算前一天的23點45分。
上面的代碼輸出:2011-02-02 00:15:00 2011-02-01 23:45:00。 其中back of和front of后接的數(shù)組必須大于等于0并且小于等于24。

strtotime函數(shù)的實現(xiàn)基本原理
官方文檔對于strtotime函數(shù)的說明是這樣的:本函數(shù)預(yù)期接受一個包含美國英語日期格式的字符串并嘗試將其解析為 Unix 時間戳 (自 January 1 1970 00:00:00 GMT 起的秒數(shù)),其值相對于 now 參數(shù)給出的時間,如果沒有提供此參數(shù)則用系統(tǒng)當(dāng)前時間。

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

復(fù)制代碼 代碼如下:

PHP_FUNCTION(strtotime)
    //  處理輸入,對于是否有第二個參數(shù)有沒的處理

    //  調(diào)用相關(guān)函數(shù),實現(xiàn)字符串的解析和結(jié)果計算

    //  返回結(jié)果
}


在輸入處理中,先識別兩個參數(shù)都存在的情況并進(jìn)行處理,如果不是此種狀態(tài),則處理第二個參數(shù)不存在的情況, 如果都沒有,則報錯,返回FALSE。

strtotime函數(shù)的第一個參數(shù)是一個字符串,對于這個字符串,由于其復(fù)雜性,PHP使用了其詞法解析一樣的工具:re2c。在/ext/date/lib目錄下,從parse_date.re文件我們可以看到其原始的re文件。 當(dāng)用戶以參數(shù)的形式傳入一個字符串,此字符串將交給此程序處理,針對其字符串的不同,匹配不同的處理函數(shù)。 如strtotime(“yesterday”)調(diào)用,分析字符串時,將匹配yesterday字符串,此字符串對應(yīng)函數(shù)如下:

復(fù)制代碼 代碼如下:

'yesterday'
{
    DEBUG_OUTPUT("yesterday");
    TIMELIB_INIT;
    TIMELIB_HAVE_RELATIVE();
    TIMELIB_UNHAVE_TIME();

    s->time->relative.d = -1;
    TIMELIB_DEINIT;
    return TIMELIB_RELATIVE;
}


這里有幾個關(guān)鍵的結(jié)構(gòu)體:
復(fù)制代碼 代碼如下:

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ù)是-1。 這只是中間詞法解析的中間結(jié)果,但是最后結(jié)果是通過這些中間結(jié)果計算出來的。

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

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

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

復(fù)制代碼 代碼如下:

reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year') 's'?) | 'weeks' | daytext;

relnumber = ([+-]*[ \t]*[0-9]+);
relative = relnumber space? (reltextunit | 'week' );


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

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

如上代碼,則是直接記錄月份的相對值減一。 但是對于類似于3月31號這樣的情況,2月沒有31號,程序會自動將日期計算到下一個月。

相關(guān)文章

  • PHP十六進(jìn)制顏色隨機(jī)生成器功能示例

    PHP十六進(jìn)制顏色隨機(jī)生成器功能示例

    這篇文章主要介紹了PHP十六進(jìn)制顏色隨機(jī)生成器功能,結(jié)合具體實例形式分析了php隨機(jī)生成十六進(jìn)制數(shù)值表示形式的相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • windows服務(wù)器使用IIS時thinkphp搜索中文無效問題

    windows服務(wù)器使用IIS時thinkphp搜索中文無效問題

    在用ThinkPHP開發(fā)的網(wǎng)站,在linux服務(wù)器下使用過一段時間,一切正常。但是更換到windows服務(wù)器時,發(fā)現(xiàn)搜索的時候,無法搜索中文,查不出相應(yīng)的結(jié)果。查看數(shù)據(jù)庫發(fā)現(xiàn)數(shù)據(jù)是存在的。linux服務(wù)器下正常,而且搜索數(shù)字或字母程序正常,說明程序是沒有任何問題的。
    2023-06-06
  • PHP7 mongoDB擴(kuò)展使用的方法分享

    PHP7 mongoDB擴(kuò)展使用的方法分享

    這篇文章主要給大家介紹了關(guān)于PHP7 mongoDB擴(kuò)展使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用PHP7具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • PHP實現(xiàn)檢測客戶端是否使用代理服務(wù)器及其匿名級別

    PHP實現(xiàn)檢測客戶端是否使用代理服務(wù)器及其匿名級別

    這篇文章主要介紹了PHP實現(xiàn)檢測客戶端是否使用代理服務(wù)器及其匿名級別,需要的朋友可以參考下
    2015-01-01
  • Php Cookie的一個使用注意點

    Php Cookie的一個使用注意點

    這里需要提醒大家注意的一點是,在 php 中, 如果你在當(dāng)前頁面設(shè)置了COOKIE,在 當(dāng)前頁面刷新 或 轉(zhuǎn)向到其他頁面之前, cookie 的設(shè)置結(jié)果不會生效(cookie未過期的情況下)。
    2008-11-11
  • php通過exif_read_data函數(shù)獲取圖片的exif信息

    php通過exif_read_data函數(shù)獲取圖片的exif信息

    這篇文章主要介紹了php通過exif_read_data函數(shù)獲取圖片的exif信息,默認(rèn)情況下,PHP讀取圖片Exif信息模塊是不開啟的,我們需要先開啟這個模塊。開啟Exif模塊需要mbstring支持,這里就不詳細(xì)說明了,我們來先看下函數(shù)的用法
    2015-05-05
  • 淺析PHP的ASCII碼轉(zhuǎn)換類

    淺析PHP的ASCII碼轉(zhuǎn)換類

    本篇文章是對PHP的ASCII碼轉(zhuǎn)換類進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • php rmdir使用遞歸函數(shù)刪除非空目錄實例詳解

    php rmdir使用遞歸函數(shù)刪除非空目錄實例詳解

    我們大家都知道,php rmdir()函數(shù)用于刪除空目錄,但如果要刪除非空目錄,我們必須將非空目錄中的文件或子目錄刪除,本文章向大家介紹php如何使用遞歸函數(shù)刪除非空目錄,需要的朋友可以參考一下
    2016-10-10
  • php $_ENV為空的原因分析

    php $_ENV為空的原因分析

    $_ENV記載了一些系統(tǒng)環(huán)境變量(因為牽扯到實際的操作系統(tǒng),所以不可能給出$_ENV的完整列表)。
    2009-06-06
  • PHP實現(xiàn)的mysql讀寫分離操作示例

    PHP實現(xiàn)的mysql讀寫分離操作示例

    這篇文章主要介紹了PHP實現(xiàn)的mysql讀寫分離操作,簡單講述了mysql讀寫分離的原理,并結(jié)合實例形式給出了php針對mysql的讀寫sql語句操作不同數(shù)據(jù)庫的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05

最新評論

读书| 喜德县| 鹤山市| 东辽县| 渝北区| 安宁市| 资兴市| 鄂伦春自治旗| 石阡县| 阆中市| 德阳市| 鄄城县| 呼和浩特市| 海淀区| 永靖县| 东方市| 宕昌县| 克东县| 仁化县| 景洪市| 和顺县| 高要市| 清苑县| 金寨县| 上饶县| 大埔县| 广汉市| 镇平县| 龙川县| 闽侯县| 凤城市| 高淳县| 南皮县| 泊头市| 馆陶县| 浮梁县| 灵石县| 福安市| 门源| 马关县| 县级市|