php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6399 checkdate should be able to validate a time as well as a date (timestamp)
Submitted: 2000-08-28 08:45 UTC Modified: 2015-02-17 07:54 UTC
Votes:21
Avg. Score:3.9 ± 1.1
Reproduced:13 of 13 (100.0%)
Same Version:6 (46.2%)
Same OS:7 (53.8%)
From: juhl at eisenstein dot dk Assigned:
Status: Wont fix Package: Date/time related
PHP Version: * OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: juhl at eisenstein dot dk
New email:
PHP Version: OS:

 

 [2000-08-28 08:45 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-12 09:53 UTC] 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);
 [2010-11-19 00:11 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related -PHP Version: 4.0 Latest CVS (28/08/2000) +PHP Version: *
 [2015-02-17 07:54 UTC] krakjoe@php.net
-Status: Open +Status: Wont fix
 [2015-02-17 07:54 UTC] krakjoe@php.net
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.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC