PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析_PHP教程
推薦:淺談php命令行用法這篇文章主要介紹了淺談php命令行用法的相關(guān)資料,需要的朋友可以參考下 Php是一個(gè)非常流行的web服務(wù)端腳本語言。其實(shí),php不僅僅可以在web服務(wù)器中充當(dāng)重要角色。在命令行一樣可以執(zhí)行。 本文中,筆者為各位介紹下php在命令行中的使用方法。 1、 查看php的版本、配置
這篇文章主要介紹了PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析,本文講解了strtotime函數(shù)的一些用法、strtotime函數(shù)的實(shí)現(xiàn)基本原理、strtotime(“-1 month”)求值失敗的原因等內(nèi)容,需要的朋友可以參考下
源碼位置:\ext\date\php_date.c
代碼如下:
/* {{{ 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”, ×, &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”, ×, &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”)求上一個(gè)月的今天時(shí)會(huì)出一些狀況,
因此也引出寫這篇文章,本文包括如下內(nèi)容:
1).strtotime函數(shù)的一些用法
2).strtotime函數(shù)的實(shí)現(xiàn)基本原理
3).strtotime(“-1 month”)求值失敗的原因
strtotime函數(shù)的一些用法
1、 strtotime(“JAN”)和strtotime(“January”)
這兩個(gè)用法的效果是一樣的,都是返回指定月份的今天,如果指定月份沒有今天,則順延到下一個(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。 從表象來看,這個(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"))), "
";
在PHP的源碼中,對(duì)于first與星期和天的組合使用是分開的,即first day對(duì)應(yīng)一個(gè)處理操作, 在最終的C實(shí)現(xiàn)中,天的值指定為1,即time結(jié)構(gòu)中的d字段指定為1,如下代碼:
代碼如下:
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)鍵字可以與星期,天組合使用,表示指定時(shí)間的前一個(gè)星期幾或前一天。如下所示代碼:
復(fù)制代碼 代碼如下:
echo date("Y-m-d H:i:s", strtotime("previous sunday", strtotime("2011-02-01"))), "
";
程序會(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"))), "
";
程序會(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"))), "
";
first、previous、last和this關(guān)鍵字在re文件中屬于同一組。
5、 back和front關(guān)鍵字
這兩個(gè)關(guān)鍵字是對(duì)一天中的小時(shí)的向前和向后操作,其調(diào)用格式如下:
代碼如下:
echo date("Y-m-d H:i:s", strtotime("back of 24", strtotime("2011-02-01"))), "
";
echo date("Y-m-d H:i:s", strtotime("front of 24", strtotime("2011-02-01"))), "
";
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)基本原理
分享: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)鏈接:
- 教程說明:
PHP教程-PHP strtotime函數(shù)用法、實(shí)現(xiàn)原理和源碼分析
。