php - mktime forcing date to December 1969 -
i having interesting problem mktime function. looked @ solutions offered on here prior , none seemed resolved or helped (strotime doesn't change results see).
i have monthly report trying make error-proof possible users, i'm requesting month , year. provide best day report in code.
my form has drop-down selection months, , values month two-digit integers (01, 02, etc). year four-digit, filled out manually user.
every works great until year 2038... know, know, it's long way off, project supposed have 20-year scope , in last 2 years, i'm running issues.
here's how i'm receiving information form:
$month = filter_var($_post["month"], filter_sanitize_number_int); $year = filter_var($_post["year"], filter_sanitize_number_int);
then, use mktime combine month , year day (27th): $timestamp = mktime(0,0,0,$month,27,$year);
then assign variable in date form mysql likes: $reportdate = date('y-m-j',$timestamp);
i've echoed out results of $month , $year , coming across correctly, when echo out $reportdate, says 12/31/1969.
03 2038 1969-12-31
it doesn't matter month choose. december 2037 reports perfectly, beyond fails.
is fixable or going have that's way cookie crumbles...?
this because timestamp overflowing, , caused year 2038 problem.
basically, integers represented signed 32-bits numbers, can hold maximum value of 2147483648. so, in unix timestamp, amount in seconds, 68 years.
in fact, google tells me:
(2^31) * seconds = 68.0511039 years
so, unix timestamp time since unix epoch, jan 1st 1970 00:00:00 utc, meaning largest date can represented in 32-bit unix timestamp have year of:
1970 + ~68 = ~2038.
if need support these dates, use datetime
class, not have such restriction, so:
$date = new datetime( "now", new datetimezone( 'america/new_york')); $date->setdate( $year, $month, 27); // $date->settime( 0, 0, 0); echo $date->format('y-m-j');
Comments
Post a Comment