1

I understand there is an issue with php mktime(), 32bit systems, and years <1901 or >2038, however, my question is, does this issue still remain if operating on a 64 bit system?

I used the code mentioned here and determined my host is running a 64bit system.

I am gathering dates from user inputs in the following format:

$m = user selected month (2 digits); $d = user selected day (2 digits); $y = user selected year (4 digits)

Here is the code I am using to convert the input date into a unix timestamp:

$npt_date=mktime(0,0,0,$m,$d,$y);

And the code to then re-display the date in the basic format xx-xx-xxxx

$date_str=date('m-d-Y',$npt_date);

The code works fine for dates > 1901, however, when $y < 1901, the output from the date() function returns the incorrect date.

Any advice as to what I am doing wrong, if this is even possible using the mktime() and date() functions, and/or possible workaround would be greatly appreciated.

Thanks in advance.

Community
  • 1
  • 1
IIIOXIII
  • 980
  • 3
  • 15
  • 30
  • Possible duplicate: http://stackoverflow.com/questions/2449380/how-can-i-work-with-dates-before-1900-in-php – Jon Oct 16 '12 at 07:27

2 Answers2

1

It might be a good idea to start using DateTime. It's only limited backwords by the year 1000.
Also time() and date() are limited by 2038 in the future, whereas DateTime won't have problem with the future either.
Here is some reading on the topic.

Havelock
  • 6,913
  • 4
  • 34
  • 42
  • I should have specified that I am running PHP 5.2. Am I correct that the DateTime::getTimestamp() is only available when running >=5.3? I assume the best workaround would be to store dates that will not be used in arithmetic in their basic xx-xx-xxxx string format if so? I really don't NEED to store them as integers; basically just echoing them out in a few places. In any case, I will mark this as the accepted solution as it would have likely worked had i been running PHP5.3+. Thanks for the quick response. – IIIOXIII Oct 16 '12 at 08:04
  • According to the PHP Doc (see the link in my answer) `DateTime` is available since PHP 5.2 – Havelock Oct 16 '12 at 08:07
  • Yeah. I saw that, and had high hopes, however the DateTime::getTimestamp() portion of DateTime seems to be >=5.3 according to [this](http://www.php.net/manual/en/datetime.gettimestamp.php), unless I have misunderstood something. In any case, I am sure I will be upgrading to PHP5.3 at some point so the advice still helped. Thanks. – IIIOXIII Oct 16 '12 at 08:24
  • @XCHANGE, you know there's the `date_*` functions which are the procedural equivalent of the OOP version. So you can use [`date_format()`](http://www.php.net/manual/en/function.date-format.php) instead and according to the PHP doc it "only" requires PHP 5.2 ;) – Havelock Oct 16 '12 at 10:41
0

Presumably, if you are storing more recent dates, you will never need more than 256 possible year values, which fits exactly into one byte. This saves you significant amounts of space: rather than using multiple bytes to store the integer 1901, 1900 years of which can be considered redundant in many cases, MySQL internally treats it as just the number 1, and displays it at 1901 for your benefit.

If you need more years, use an INT-based type.

also refere this Does MySQL support historical date (like 1200)?

Community
  • 1
  • 1
M.I.T.
  • 1,040
  • 2
  • 17
  • 34