Req #6399 [Opn->Wfx]: checkdate should be able to validate a time as well as a date (timestamp)

From: Date: Tue, 17 Feb 2015 07:54:11 +0000
Subject: Req #6399 [Opn->Wfx]: checkdate should be able to validate a time as well as a date (timestamp)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190728@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=6399&edit=1 ID: 6399 Updated by: krakjoe@php.net Reported by: juhl at eisenstein dot dk Summary: checkdate should be able to validate a time as well as a date (timestamp) -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Date/time related Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: This has been open for a long long time, sorry about the wait. There are too many interpretations of the implementation of this idea that there is no way for us to pick one, and for the function to have configurable behaviour at runtime would be an absolute nightmare. I'm going to mark as won't fix. Previous Comments: ------------------------------------------------------------------------ [2007-08-12 09:53:16] shahar dot evron at gmail dot com 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); ------------------------------------------------------------------------ [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 juhl@eisenstein.dk ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=6399&edit=1

« previous php.bugs (#190728) next »