ID: 6399
Comment by: shahar dot evron at gmail dot com
Reported By: juhl at eisenstein dot dk
Status: Open
Bug Type: Feature/Change Request
Operating System: *
PHP Version: 4.0 Latest CVS (28/08/2000)
New Comment:
Don't know if it helps anyone - but I created a function similar to
checkdate() called checktime() that can be used like this:
bool checktime(int $hour, int $minute [, int $second [, bool $natime
]]);
$natime defaults to FALSE, but if TRUE, the function assumes
North-American style hours are used - so only 1 - 12 is valid (and not 0
- 23).
So:
checktime(23, 12) // true - 23:12 is ok
checktime(24, 61, 67) // false - no such time 24:61:67
checktime(18, 43, 30, true) // false - NA Time only allows hour 01 -
12
etc.
Patch against PHP 5.2 HEAD follows.
Shahar.
--- snip ---
Index: ext/date/php_date.c
===================================================================
RCS file: /repository/php-src/ext/date/php_date.c,v
retrieving revision 1.43.2.45.2.51
diff -u -r1.43.2.45.2.51 php_date.c
--- ext/date/php_date.c 12 Jul 2007 18:59:05 -0000 1.43.2.45.2.51
+++ ext/date/php_date.c 10 Aug 2007 17:04:21 -0000
@@ -83,6 +83,14 @@
ZEND_END_ARG_INFO()
static
+ZEND_BEGIN_ARG_INFO_EX(arginfo_checktime, 0, 0, 2)
+ ZEND_ARG_INFO(0, hour)
+ ZEND_ARG_INFO(0, minute)
+ ZEND_ARG_INFO(0, second)
+ ZEND_ARG_INFO(0, natime)
+ZEND_END_ARG_INFO()
+
+static
ZEND_BEGIN_ARG_INFO_EX(arginfo_strftime, 0, 0, 1)
ZEND_ARG_INFO(0, format)
ZEND_ARG_INFO(0, timestamp)
@@ -156,6 +164,7 @@
PHP_FE(mktime, arginfo_mktime)
PHP_FE(gmmktime, arginfo_gmmktime)
PHP_FE(checkdate, arginfo_checkdate)
+ PHP_FE(checktime, arginfo_checktime)
#ifdef HAVE_STRFTIME
PHP_FE(strftime, arginfo_strftime)
@@ -1244,7 +1253,6 @@
}
/* }}} */
-
/* {{{ proto bool checkdate(int month, int day, int year)
Returns true(1) if it is a valid date in gregorian calendar */
PHP_FUNCTION(checkdate)
@@ -1262,6 +1270,29 @@
}
/* }}} */
+/* {{{ proto bool checktime(int hour, int minute [, int second [, bool
natime]])
+ Returns true(1) if it is a valid time */
+PHP_FUNCTION(checktime)
+{
+ long h, m, s = 0;
+ zend_bool natime = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|lb",
&h, &m, &s, &natime) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (h < 0 || h > 23 || m < 0 || m > 59 || s < 0 || s > 59) {
+ RETURN_FALSE;
+ }
+
+ if (natime && (h < 1 || h > 12)) {
+ RETURN_FALSE;
+ }
+
+ RETURN_TRUE; /* True : The hour, minute, second make a valid
time */
+}
+/* }}} */
+
#ifdef HAVE_STRFTIME
/* {{{ php_strftime - (gm)strftime helper */
PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
Index: ext/date/php_date.h
===================================================================
RCS file: /repository/php-src/ext/date/php_date.h,v
retrieving revision 1.17.2.11.2.3
diff -u -r1.17.2.11.2.3 php_date.h
--- ext/date/php_date.h 1 Jan 2007 09:35:48 -0000 1.17.2.11.2.3
+++ ext/date/php_date.h 10 Aug 2007 17:04:21 -0000
@@ -36,6 +36,7 @@
PHP_FUNCTION(gmmktime);
PHP_FUNCTION(checkdate);
+PHP_FUNCTION(checktime);
#ifdef HAVE_STRFTIME
PHP_FUNCTION(strftime);
Previous Comments:
------------------------------------------------------------------------
[2000-08-28 08:45:24] juhl at eisenstein dot dk
It would be nice if checkdate could validate a time as well as a date.
Currently you can only check that a given month/day/year combination is
valid. if this could be extended to validate
month/day/year/hour/minute/second (possibly taking leap-seconds into
acount) it would make it much easier to validate dates and times that a
user enters before doing anything (like storing it in a db) with it, as
everything could be checked with a single call.
And it would be nice if you could just pass NULL or something similar
to ignore one or more fields, so that the following examples would all
be valid:
checkdate(3, 15, 2000, NULL, NULL, NULL); // only validate date
checkdate(NULL, NULL, NULL, 16, 05, 31); // only validate time
checkdate(3, 15, 2000, 16, 05, 31); // validate date and time
checkdate(NULL, 15, NULL, 16, NULL, NULL); // validate partial date and
partial time
Best regards,
Jesper Juhl
[EMAIL PROTECTED]
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=6399&edit=1