📄 datetime.c
字号:
case 'n': /* month, numeric, no leading zeros */ sprintf(tmp_buff, "%d", ta->tm_mon + 1); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'd': /* day of the month, numeric */ sprintf(tmp_buff, "%02d", ta->tm_mday); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'j': sprintf(tmp_buff, "%d", ta->tm_mday); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'H': /* hour, numeric, 24 hour format */ sprintf(tmp_buff, "%02d", ta->tm_hour); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'h': /* hour, numeric, 12 hour format */ h = ta->tm_hour % 12; if (h==0) h = 12; sprintf(tmp_buff, "%02d", h); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'G': /* hour, numeric, 24 hour format, no leading zeros */ sprintf(tmp_buff, "%d", ta->tm_hour); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'g': /* hour, numeric, 12 hour format, no leading zeros */ h = ta->tm_hour % 12; if (h==0) h = 12; sprintf(tmp_buff, "%d", h); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'i': /* minutes, numeric */ sprintf(tmp_buff, "%02d", ta->tm_min); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 's': /* seconds, numeric */ sprintf(tmp_buff, "%02d", ta->tm_sec); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'A': /* AM/PM */ strcat(Z_STRVAL_P(return_value), (ta->tm_hour >= 12 ? "PM" : "AM")); break; case 'a': /* am/pm */ strcat(Z_STRVAL_P(return_value), (ta->tm_hour >= 12 ? "pm" : "am")); break; case 'S': /* standard english suffix, e.g. 2nd/3rd for the day of the month */ if (ta->tm_mday >= 10 && ta->tm_mday <= 19) { strcat(Z_STRVAL_P(return_value), "th"); } else { switch (ta->tm_mday % 10) { case 1: strcat(Z_STRVAL_P(return_value), "st"); break; case 2: strcat(Z_STRVAL_P(return_value), "nd"); break; case 3: strcat(Z_STRVAL_P(return_value), "rd"); break; default: strcat(Z_STRVAL_P(return_value), "th"); break; } } break; case 't': /* days in current month */ sprintf(tmp_buff, "%2d", phpday_tab[isleap((ta->tm_year+YEAR_BASE))][ta->tm_mon] ); strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'w': /* day of the week, numeric EXTENSION */ sprintf(tmp_buff, "%01d", ta->tm_wday); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'O': /* GMT offset in [+-]HHMM format */#if HAVE_TM_GMTOFF sprintf(tmp_buff, "%c%02d%02d", (ta->tm_gmtoff < 0) ? '-' : '+', abs(ta->tm_gmtoff / 3600), abs( (ta->tm_gmtoff % 3600) / 60 ));#else sprintf(tmp_buff, "%c%02d%02d", ((ta->tm_isdst ? tzone - 3600:tzone)>0)?'-':'+', abs((ta->tm_isdst ? tzone - 3600 : tzone) / 3600), abs(((ta->tm_isdst ? tzone - 3600 : tzone) % 3600) / 60));#endif strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'Z': /* timezone offset in seconds */#if HAVE_TM_GMTOFF sprintf(tmp_buff, "%ld", ta->tm_gmtoff);#else sprintf(tmp_buff, "%ld", ta->tm_isdst ? -(tzone- 3600) : -tzone);#endif strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'L': /* boolean for leapyear */ sprintf(tmp_buff, "%d", (isleap((ta->tm_year+YEAR_BASE)) ? 1 : 0 ) ); strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'T': /* timezone name */#if HAVE_TM_ZONE strcat(Z_STRVAL_P(return_value), ta->tm_zone);#elif HAVE_TZNAME strcat(Z_STRVAL_P(return_value), ta->tm_isdst ? tname[1] : tname[0]);#endif break; case 'B': /* Swatch Beat a.k.a. Internet Time */ beat = (((((long)the_time)-(((long)the_time) - ((((long)the_time) % 86400) + 3600))) * 10) / 864); while (beat < 0) { beat += 1000; } beat = beat % 1000; sprintf(tmp_buff, "%03d", beat); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'I': sprintf(tmp_buff, "%d", ta->tm_isdst); strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'r':#if HAVE_TM_GMTOFF sprintf(tmp_buff, "%3s, %02d %3s %04d %02d:%02d:%02d %c%02d%02d", day_short_names[ta->tm_wday], ta->tm_mday, mon_short_names[ta->tm_mon], ta->tm_year + YEAR_BASE, ta->tm_hour, ta->tm_min, ta->tm_sec, (ta->tm_gmtoff < 0) ? '-' : '+', abs(ta->tm_gmtoff / 3600), abs( (ta->tm_gmtoff % 3600) / 60 ) );#else sprintf(tmp_buff, "%3s, %02d %3s %04d %02d:%02d:%02d %c%02d%02d", day_short_names[ta->tm_wday], ta->tm_mday, mon_short_names[ta->tm_mon], ta->tm_year + YEAR_BASE, ta->tm_hour, ta->tm_min, ta->tm_sec, ((ta->tm_isdst ? tzone - 3600 : tzone) > 0) ? '-' : '+', abs((ta->tm_isdst ? tzone - 3600 : tzone) / 3600), abs( ((ta->tm_isdst ? tzone - 3600 : tzone) % 3600) / 60 ) );#endif strcat(Z_STRVAL_P(return_value), tmp_buff); break; case 'W': /* ISO-8601 week number of year, weeks starting on Monday */ wd = ta->tm_wday == 0 ? 6 : ta->tm_wday - 1; /* weekday */ yd = ta->tm_yday + 1; /* days since January 1st */ fd = (7 + wd - yd % 7+ 1) % 7; /* weekday (1st January) */ /* week is a last year week (52 or 53) */ if ((yd <= 7 - fd) && fd > 3){ wk = (fd == 4 || (fd == 5 && isleap((ta->tm_year + YEAR_BASE - 1)))) ? 53 : 52; } /* week is a next year week (1) */ else if (isleap((ta->tm_year+YEAR_BASE)) + 365 - yd < 3 - wd){ wk = 1; } /* normal week */ else { wk = (yd + 6 - wd + fd) / 7 - (fd > 3); } sprintf(tmp_buff, "%02d", wk); /* SAFE */ strcat(Z_STRVAL_P(return_value), tmp_buff); break; default: length = strlen(Z_STRVAL_P(return_value)); Z_STRVAL_P(return_value)[length] = Z_STRVAL_PP(format)[i]; Z_STRVAL_P(return_value)[length + 1] = '\0'; break; } } Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); Z_TYPE_P(return_value) = IS_STRING;}/* }}} *//* {{{ proto string date(string format [, int timestamp]) Format a local time/date */PHP_FUNCTION(date){ php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);}/* }}} *//* {{{ proto string gmdate(string format [, int timestamp]) Format a GMT/UTC date/time */PHP_FUNCTION(gmdate){ php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);}/* }}} *//* {{{ proto array localtime([int timestamp [, bool associative_array]]) Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array */PHP_FUNCTION(localtime){ zval **timestamp_arg, **assoc_array_arg; struct tm *ta, tmbuf; time_t timestamp; int assoc_array = 0; int arg_count = ZEND_NUM_ARGS(); if (arg_count < 0 || arg_count > 2 || zend_get_parameters_ex(arg_count, ×tamp_arg, &assoc_array_arg) == FAILURE) { WRONG_PARAM_COUNT; } switch (arg_count) { case 0: timestamp = (long)time(NULL); break; case 1: convert_to_long_ex(timestamp_arg); timestamp = Z_LVAL_PP(timestamp_arg); break; case 2: convert_to_long_ex(timestamp_arg); convert_to_long_ex(assoc_array_arg); timestamp = Z_LVAL_PP(timestamp_arg); assoc_array = Z_LVAL_PP(assoc_array_arg); break; } #ifdef PHP_WIN32 if (timestamp < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Windows does not support negative values for this function"); RETURN_FALSE }#endif if (NULL == (ta = php_localtime_r(×tamp, &tmbuf))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid local time"); RETURN_FALSE; } array_init(return_value); if (assoc_array) { add_assoc_long(return_value, "tm_sec", ta->tm_sec); add_assoc_long(return_value, "tm_min", ta->tm_min); add_assoc_long(return_value, "tm_hour", ta->tm_hour); add_assoc_long(return_value, "tm_mday", ta->tm_mday); add_assoc_long(return_value, "tm_mon", ta->tm_mon); add_assoc_long(return_value, "tm_year", ta->tm_year); add_assoc_long(return_value, "tm_wday", ta->tm_wday); add_assoc_long(return_value, "tm_yday", ta->tm_yday); add_assoc_long(return_value, "tm_isdst", ta->tm_isdst); } else { add_next_index_long(return_value, ta->tm_sec); add_next_index_long(return_value, ta->tm_min); add_next_index_long(return_value, ta->tm_hour); add_next_index_long(return_value, ta->tm_mday); add_next_index_long(return_value, ta->tm_mon); add_next_index_long(return_value, ta->tm_year); add_next_index_long(return_value, ta->tm_wday); add_next_index_long(return_value, ta->tm_yday); add_next_index_long(return_value, ta->tm_isdst); }}/* }}} *//* {{{ proto array getdate([int timestamp]) Get date/time information */PHP_FUNCTION(getdate){ pval **timestamp_arg; struct tm *ta, tmbuf; time_t timestamp; if (ZEND_NUM_ARGS() == 0) { timestamp = time(NULL); } else if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, ×tamp_arg) == FAILURE) { WRONG_PARAM_COUNT; } else { convert_to_long_ex(timestamp_arg); timestamp = Z_LVAL_PP(timestamp_arg); } ta = php_localtime_r(×tamp, &tmbuf); if (!ta) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot perform date calculation"); return; } array_init(return_value); add_assoc_long(return_value, "seconds", ta->tm_sec); add_assoc_long(return_value, "minutes", ta->tm_min); add_assoc_long(return_value, "hours", ta->tm_hour); add_assoc_long(return_value, "mday", ta->tm_mday); add_assoc_long(return_value, "wday", ta->tm_wday); add_assoc_long(return_value, "mon", ta->tm_mon + 1); add_assoc_long(return_value, "year", ta->tm_year + 1900); add_assoc_long(return_value, "yday", ta->tm_yday); add_assoc_string(return_value, "weekday", day_full_names[ta->tm_wday], 1); add_assoc_string(return_value, "month", mon_full_names[ta->tm_mon], 1); add_index_long(return_value, 0, timestamp);}/* }}} *//* {{{ php_std_date Return date string in standard format for http headers */char *php_std_date(time_t t TSRMLS_DC){ struct tm *tm1, tmbuf; char *str; tm1 = php_gmtime_r(&t, &tmbuf); str = emalloc(81); if (PG(y2k_compliance)) { snprintf(str, 80, "%s, %02d %s %04d %02d:%02d:%02d GMT", day_short_names[tm1->tm_wday], tm1->tm_mday, mon_short_names[tm1->tm_mon], tm1->tm_year + 1900, tm1->tm_hour, tm1->tm_min, tm1->tm_sec); } else { snprintf(str, 80, "%s, %02d-%s-%02d %02d:%02d:%02d GMT", day_full_names[tm1->tm_wday], tm1->tm_mday, mon_short_names[tm1->tm_mon], ((tm1->tm_year) % 100), tm1->tm_hour, tm1->tm_min, tm1->tm_sec); } str[79] = 0; return (str);}/* }}} *//* {{{ proto bool checkdate(int month, int day, int year) Returns true(1) if it is a valid date in gregorian calendar */PHP_FUNCTION(checkdate){ long m, d, y; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &m, &d, &y) == FAILURE) { RETURN_FALSE; } if (y < 1 || y > 32767 || m < 1 || m > 12 || d < 1 || d > phpday_tab[isleap(y)][m - 1]) { RETURN_FALSE; } RETURN_TRUE; /* True : This month, day, year arguments are valid */}/* }}} */#if HAVE_STRFTIME/* {{{ _php_strftime */void _php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gm){ pval **format_arg, **timestamp_arg; char *format, *buf; time_t timestamp; struct tm *ta, tmbuf; int max_reallocs = 5; size_t buf_len=64, real_len; switch (ZEND_NUM_ARGS()) { case 1: if (zend_get_parameters_ex(1, &format_arg)==FAILURE) { RETURN_FALSE; } time(×tamp); break; case 2: if (zend_get_parameters_ex(2, &format_arg, ×tamp_arg)==FAILURE) { RETURN_FALSE; } convert_to_long_ex(timestamp_arg); timestamp = Z_LVAL_PP(timestamp_arg); break; default: WRONG_PARAM_COUNT; break; } convert_to_string_ex(format_arg); if (Z_STRLEN_PP(format_arg)==0) { RETURN_FALSE; }#ifdef PHP_WIN32 if (timestamp < 0) { RETURN_FALSE; }#endif format = Z_STRVAL_PP(format_arg); if (gm) { ta = php_gmtime_r(×tamp, &tmbuf); } else { ta = php_localtime_r(×tamp, &tmbuf); } buf = (char *) emalloc(buf_len); while ((real_len=strftime(buf, buf_len, format, ta))==buf_len || real_len==0) { buf_len *= 2; buf = (char *) erealloc(buf, buf_len); if (!--max_reallocs) { break; } } if (real_len && real_len != buf_len) { buf = (char *) erealloc(buf, real_len + 1); RETURN_STRINGL(buf, real_len, 0); } efree(buf); RETURN_FALSE;}/* }}} *//* {{{ proto string strftime(string format [, int timestamp]) Format a local time/date according to locale settings */PHP_FUNCTION(strftime){ _php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);}/* }}} *//* {{{ proto string gmstrftime(string format [, int timestamp]) Format a GMT/UCT time/date according to locale settings */PHP_FUNCTION(gmstrftime){ _php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);}/* }}} */#endif/* {{{ proto int strtotime(string time, int now) Convert string representation of date and time to a timestamp */PHP_FUNCTION(strtotime){ zval **z_time, **z_now; int argc; time_t now; argc = ZEND_NUM_ARGS(); if (argc < 1 || argc > 2 || zend_get_parameters_ex(argc, &z_time, &z_now)==FAILURE) { WRONG_PARAM_COUNT; } convert_to_string_ex(z_time); if (Z_STRLEN_PP(z_time) == 0) php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Called with empty time parameter"); if (argc == 2) { convert_to_long_ex(z_now); now = Z_LVAL_PP(z_now); RETURN_LONG(php_parse_date(Z_STRVAL_PP(z_time), &now)); } else { RETURN_LONG(php_parse_date(Z_STRVAL_PP(z_time), NULL)); }}/* }}} *//* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: sw=4 ts=4 fdm=marker * vim<600: sw=4 ts=4 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -