>  기사  >  php教程  >  PHP strtotime函数用法、实现原理和源码分析

PHP strtotime函数用法、实现原理和源码分析

WBOY
WBOY원래의
2016-06-06 20:11:13855검색

这篇文章主要介绍了PHP strtotime函数用法、实现原理和源码分析,本文讲解了strtotime函数的一些用法、strtotime函数的实现基本原理、strtotime(“-1 month”)求

源码位置:\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函数在使用strtotime(“-1 month”)求上一个月的今天时会出一些状况,

因此也引出写这篇文章,本文包括如下内容:

1).strtotime函数的一些用法
2).strtotime函数的实现基本原理
3).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"))), "
";


在PHP的源码中,对于first与星期和天的组合使用是分开的,即first day对应一个处理操作, 在最终的C实现中,天的值指定为1,即time结构中的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关键字

与first类似,previous关键字可以与星期,天组合使用,表示指定时间的前一个星期几或前一天。如下所示代码:

复制代码 代码如下:

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.