PHP strtotime函数用法、实现原理和源码分析(2)_PHP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:浅谈php命令行用法
这篇文章主要介绍了浅谈php命令行用法的相关资料,需要的朋友可以参考下 Php是一个非常流行的web服务端脚本语言。其实,php不仅仅可以在web服务器中充当重要角色。在命令行一样可以执行。 本文中,笔者为各位介绍下php在命令行中的使用方法。 1、 查看php的版本、配置

  官方文档对于strtotime函数的说明是这样的:本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳 (自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。

  这是一个标准PHP内置函数,从PHP4起就已经存在。strtotime函数是以一个扩展的方式加载进来的,在ext/date目录下有其全部实现。 作为一个标准的内置函数,其定义格式也是标准的,如下:

  代码如下:

  PHP_FUNCTION(strtotime)

  // 处理输入,对于是否有第二个参数有没的处理

  // 调用相关函数,实现字符串的解析和结果计算

  // 返回结果

  }

  在输入处理中,先识别两个参数都存在的情况并进行处理,如果不是此种状态,则处理第二个参数不存在的情况, 如果都没有,则报错,返回FALSE。

  strtotime函数的第一个参数是一个字符串,对于这个字符串,由于其复杂性,PHP使用了其词法解析一样的工具:re2c。在/ext/date/lib目录下,从parse_date.re文件我们可以看到其原始的re文件。 当用户以参数的形式传入一个字符串,此字符串将交给此程序处理,针对其字符串的不同,匹配不同的处理函数。 如strtotime(“yesterday”)调用,分析字符串时,将匹配yesterday字符串,此字符串对应函数如下:

  代码如下:

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

  s->time->relative.d = -1;所表示的意思是当前时间的相对天数是-1。 这只是中间词法解析的中间结果,但是最后结果是通过这些中间结果计算出来的。

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

  虽然strtotime(“-1 month”)这种方法对于后一个月比前一个月的天数的情况会求值失败,但是从其本质上来说,这并没有错。 PHP这样实现也无可厚非。只是我们的需求决定了我们不能使用这种方法,因此我们称其为求值失败。

  我们来看它的实现过程,由于没有第二个参数,所以程序使用默认的当前时间。 第一个参数传入的是-1 month字符串,这个字符串所对应的re文件中的正则为:

  代码如下:

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

  relnumber = ([+-]*[ \t]*[0-9]+);

  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号,程序会自动将日期计算到下一个月。

分享:php curl登陆qq后获取用户信息时证书错误
这篇文章主要介绍了php curl登陆qq后获取用户信息时证书错误,需要的朋友可以参考下 今晚开放ecmall商城的QQ登陆功能,在回调时产生错误,file_get_contents函数执行时,没有抓取到正确的信息,于是改用curl,但是提示证书错误。 在网上找到了解决方法,就是去掉证书认

共2页上一页12下一页
来源:模板无忧//所属分类:PHP教程/更新时间:2015-02-05
相关PHP教程