Req #47806 [Opn->Fbk]: mktime should return -1 or false on invalid args, but returns 943912800
| From: | cmb@php.net | Date: | Fri, 01 May 2015 19:48:43 +0000 |
| Subject: | Req #47806 [Opn->Fbk]: mktime should return -1 or false on invalid args, but returns 943912800 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192446@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=47806&edit=1
ID: 47806
Updated by: cmb@php.net
Reported by: glen at delfi dot ee
Summary: mktime should return -1 or false on invalid args,
but returns 943912800
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: Date/time related
Operating System: PLD Linux
PHP Version: 5.2.9
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Juggling NULL to 0 when an integer is expected is normal behavior
for PHP, e.g.
echo cos(null); // 1
Just changing a single function in this respect would be
confusing, and too much of a BC break, in my opinion. However, PHP
7 is going to give the ability to switch to a stricter mode[1]
with:
declare(strict_types=1);
Then passing NULL where an integer is expected will produce
E_RECOVERABLE_ERROR. You could catch the Exception and wrap the
code in a small function; a simplified example:
<http://3v4l.org/qgp3u>
Do you deem that sufficient?
[1] <https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_of_strict_type_checks>
Previous Comments:
------------------------------------------------------------------------
[2009-03-27 12:25:48] glen at delfi dot ee
Description:
------------
http://php.net/mktime
"mktime() returns the Unix timestamp of the arguments given. If the
arguments are invalid, the function returns FALSE (before PHP 5.1 it
returned -1)."
but it returns "Tue Nov 30 00:00:00 1999 +0200" instead when invalid
input like null (or undefined) is used:
php -r 'echo mktime(0, 0, 0, null, null, null), "\n";'
943912800
ok, i understand if all params are 0, then it would make sense:
"The number of the year, may be a two or four digit value, with
values between 0-69 mapping to 2000-2069 and 70-100 to 1970-2000. On
systems where time_t is a 32bit signed integer, as most common
today, the valid range for year is somewhere between 1901 and 2038.
However, before PHP 5.1.0 this range was limited from 1970 to 2038
on some systems (e.g. Windows)."
Year as 0 is 2000,
Month 0 is calculated as 12 of the last year, thus it gets December
and 0 December is 30 November
please make using undefined variables and nulls being invalid input
(so that you must cast to int, to treat empty input as 0):
php -r 'echo mktime(0, 0, 0, (int )$_GET['day'],
(int )$_GET['month'], (int )$_GET['year']), "\n";'
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=47806&edit=1