php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47806 mktime should return -1 or false on invalid args, but returns 943912800
Submitted: 2009-03-27 12:25 UTC Modified: 2015-05-02 11:56 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:2 (40.0%)
From: glen at delfi dot ee Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: 5.2.9 OS: PLD Linux
Private report: No CVE-ID: None
 [2009-03-27 12:25 UTC] 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";'



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-23 21:18 UTC] jani@php.net
-Package: Feature/Change Request +Package: Date/time related
 [2015-05-01 19:48 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2015-05-01 19:48 UTC] cmb@php.net
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>
 [2015-05-02 08:40 UTC] glen at delfi dot ee
-Status: Feedback +Status: Assigned
 [2015-05-02 08:40 UTC] glen at delfi dot ee
it's a late reply, but okay, yes! :)
 [2015-05-02 11:56 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2015-05-02 11:56 UTC] cmb@php.net
> it's a late reply

Sorry. However, better late then never. :)

> but okay, yes! :)

Fine. Closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Feb 27 21:01:27 2024 UTC