在前面的一文中, 我们提到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")));
复制代码 代码如下:
echo date("Y-m-d H:i:s", strtotime("second sunday", strtotime("2011-01-01"))), "
";
复制代码 代码如下:
switch (time->relative.first_last_day_of) {
case 1: /* first */
time->d = 1;
break;
case 2: /* last */
time->d = 0;
time->m++;
break;
}
复制代码 代码如下:
echo date("Y-m-d H:i:s", strtotime("previous sunday", strtotime("2011-02-01"))), "
";
复制代码 代码如下:
echo date("Y-m-d H:i:s", strtotime("last sunday", strtotime("2011-02-05"))), "
";
复制代码 代码如下:
echo date("Y-m-d H:i:s", strtotime("last day", strtotime("2000-02-01"))), "
";
复制代码 代码如下:
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"))), "
";
复制代码 代码如下:
PHP_FUNCTION(strtotime)
// 处理输入,对于是否有第二个参数有没的处理
// 调用相关函数,实现字符串的解析和结果计算
// 返回结果
}
复制代码 代码如下:
'yesterday'
{
DEBUG_OUTPUT("yesterday");
TIMELIB_INIT;
TIMELIB_HAVE_RELATIVE();
TIMELIB_UNHAVE_TIME();
s->time->relative.d = -1;
TIMELIB_DEINIT;
return TIMELIB_RELATIVE;
}
复制代码 代码如下:
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;
复制代码 代码如下:
reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year') 's'?) | 'weeks' | daytext;
relnumber = ([+-]*[ /t]*[0-9]+);
relative = relnumber space? (reltextunit | 'week' );
复制代码 代码如下:
case TIMELIB_MONTH: s->time->relative.m += amount * relunit->multiplier; break;