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

首頁 > 語言 > PHP > 正文

深入分析PHP strtotime函數

2024-09-04 11:49:15
字體:
來源:轉載
供稿:網友

strtotime函數的使用在時間日期處理上是非常的強大了,這里我們一起來看看strtotime函數內核與常用方法.

PHP strtotime函數將任何英文文本的日期時間描述解析為Unix時間戳[將系統時間轉化成unix時間戳]

一,獲取指定日期的unix時間戳 strtotime(”2009-1-22″) 示例如下:

echo strtotime(”2009-1-22“) 結果:1232553600

說明:返回2009年1月22日0點0分0秒時間戳

二,獲取英文文本日期時間 示例如下:

便于比較,使用date將當時間戳與指定時間戳轉換成系統時間

(1)打印明天此時的時間戳strtotime(”+1 day“)

當前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25

指定時間:echo date(”Y-m-d H:i:s”,strtotime(”+1 day”)) 結果:2009-01-23 09:40:25

(2)打印昨天此時的時間戳strtotime(”-1 day“)

當前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25

指定時間:echo date(”Y-m-d H:i:s”,strtotime(”-1 day”)) 結果:2009-01-21 09:40:25

(3)打印下個星期此時的時間戳strtotime(”+1 week“)

當前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25

指定時間:echo date(”Y-m-d H:i:s”,strtotime(”+1 week”)) 結果:2009-01-29 09:40:25

(4)打印上個星期此時的時間戳strtotime(”-1 week“)

當前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25

指定時間:echo date(”Y-m-d H:i:s”,strtotime(”-1 week”)) 結果:2009-01-15 09:40:25

(5)打印指定下星期幾的時間戳strtotime(”next Thursday“)

當前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25

指定時間:echo date(”Y-m-d H:i:s”,strtotime(”next Thursday”)) 結果:2009-01-29 00:00:00

(6)打印指定上星期幾的時間戳strtotime(”last Thursday“)

當前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25

指定時間:echo date(”Y-m-d H:i:s”,strtotime(”last Thursday”)) 結果:2009-01-15 00:00:00

以上示例可知,strtotime能將任何英文文本的日期時間描述解析為Unix時間戳,我們結合mktime()或date()格式化日期時間獲取指定的時間戳,實現所需要的日期時間。

現在我們深入分析它的源碼

源碼位置:\ext\date\php_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.     timelib_time *t, *now; 
  9.     timelib_tzinfo *tzi; 
  10.     tzi = get_timezone_info(TSRMLS_C); 
  11.     if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, “sl”, &times, &time_len, &preset_ts) != FAILURE) { 
  12.         /* We have an initial timestamp */ 
  13.         now = timelib_time_ctor(); 
  14.         initial_ts = emalloc(25); 
  15.         snprintf(initial_ts, 24, “@%ld UTC”, preset_ts); 
  16.         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 */ 
  17.         timelib_update_ts(t, tzi); 
  18.         now->tz_info = tzi; 
  19.         now->zone_type = TIMELIB_ZONETYPE_ID; 
  20.         timelib_unixtime2local(now, t->sse); 
  21.         timelib_time_dtor(t); 
  22.         efree(initial_ts); 
  23.     } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “s|l”, &times, &time_len, &preset_ts) != FAILURE) { 
  24.         /* We have no initial timestamp */ 
  25.         now = timelib_time_ctor(); 
  26.         now->tz_info = tzi; 
  27.         now->zone_type = TIMELIB_ZONETYPE_ID; 
  28.         timelib_unixtime2local(now, (timelib_sll) time(NULL)); 
  29.     } else { 
  30.         RETURN_FALSE; 
  31.     } 
  32.     if (!time_len) { 
  33.         timelib_time_dtor(now);     
  34.         RETURN_FALSE; 
  35.     } 
  36.     t = timelib_strtotime(times, time_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); 
  37.     error1 = error->error_count; 
  38.     timelib_error_container_dtor(error); 
  39.     timelib_fill_holes(t, now, TIMELIB_NO_CLONE); 
  40.     timelib_update_ts(t, tzi); 
  41.     ts = timelib_date_to_int(t, &error2); 
  42.     timelib_time_dtor(now); 
  43.     timelib_time_dtor(t); 
  44.     if (error1 || error2) { 
  45.         RETURN_FALSE; 
  46.     } else { 
  47.         RETURN_LONG(ts); 
  48.     } 
  49. /* }}} */ 

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

因此也引出寫這篇文章,本文包括如下內容:

strtotime函數的一些用法

strtotime函數的實現基本原理

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

strtotime函數的一些用法

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

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

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

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

2、first關鍵字

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

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

在PHP的源碼中,對于first與星期和天的組合使用是分開的,即first day對應一個處理操作,在最終的C實現中,天的值指定為1,即time結構中的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關鍵字

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

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關鍵字與previous相反,它表示下一個星期幾或后一天.

4、last關鍵字

last關鍵字既可以作為上一個,也可以作為最后一個,如求上一個星期天的日期,代碼如下:

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

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

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

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

first、previous、last和this關鍵字在re文件中屬于同一組。

5、back和front關鍵字

這兩個關鍵字是對一天中的小時的向前和向后操作,其調用格式如下:

  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表示將時間設置指定小時值的后一個小時的15分的位置,如果是24點,則算到第二天的0點15分.

front表示將時間設置指定小時值的前一個小時的45分的位置,如果是0點,則算前一天的23點45分.

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

strtotime函數的實現基本原理

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

這是一個標準PHP內置函數,從PHP4起就已經存在。strtotime函數是以一個擴展的方式加載進來的,在ext/date目錄下有其全部實現。 作為一個標準的內置函數,其定義格式也是標準的,如下:

  1. PHP_FUNCTION(strtotime
  2.     //  處理輸入,對于是否有第二個參數有沒的處理 
  3.     //  調用相關函數,實現字符串的解析和結果計算 
  4.     //  返回結果 

在輸入處理中,先識別兩個參數都存在的情況并進行處理,如果不是此種狀態,則處理第二個參數不存在的情況,如果都沒有,則報錯,返回FALSE.

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

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

這里有幾個關鍵的結構體:

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

s->time->relative.d = -1;所表示的意思是當前時間的相對天數是-1,這只是中間詞法解析的中間結果,但是最后結果是通過這些中間結果計算出來的.

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

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

我們來看它的實現過程,由于沒有第二個參數,所以程序使用默認的當前時間,第一個參數傳入的是-1 month字符串,這個字符串所對應的re文件中的正則為:

  1. reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year''s'?) | 'weeks' | daytext; 
  2. relnumber = ([+-]*[ \t]*[0-9]+); 
  3. 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號,程序會自動將日期計算到下一個月.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宽甸| 英超| 湟源县| 噶尔县| 吴忠市| 正蓝旗| 同仁县| 新宾| 林芝县| 左贡县| 河北区| 洱源县| 博湖县| 广河县| 南华县| 舟曲县| 石台县| 靖江市| 庆阳市| 长岛县| 遵义县| 朝阳县| 巧家县| 通江县| 读书| 石泉县| 改则县| 灵宝市| 渝中区| 育儿| 镇巴县| 濮阳市| 含山县| 闵行区| 清苑县| 万州区| 阳西县| 高雄市| 文水县| 阿拉善盟| 安乡县|