I have converted a date 01-01-2039 using php date() function:
echo date("d-m-Y",strtotime('01-01-2039'));
Output is shown as: 01-01-1970
Any solution for this?
I have converted a date 01-01-2039 using php date() function:
echo date("d-m-Y",strtotime('01-01-2039'));
Output is shown as: 01-01-1970
Any solution for this?
This Answer from comment conversation
The problem is caused by the Unix Timestamp Epoch problem.
The solution to this for native PHP is to use the 64bit PHP version that correctly handles this particular integer overflow.
echo (PHP_INT_SIZE===8)?"64 bit ":"32 bit ";
This above code shows the PHP version.
If 32bit PHP is present then that needs to be replaced by the 64bit PHP
In addition to @Martin's answer, I want to point out the ambiguity caused by some date formats when using strtotime
.
In the manual:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or
DateTime::createFromFormat()
when possible.
$dt = DateTime::createFromFormat('d-m-Y', '01-01-2039');
echo $dt->format('d-m-Y');