PHP mktime() for dates pre 1900
문제
So I am working on this system that is sorting dates by decades. I am using make times and I have everything working fine until it hits the year 1900 or below. Afer that everything returns a Dec 24,1964 type date. Can anyone else me why this is happening and a possbile soltuion?
And the code for this:
//$decades is a string ex: '1950-1960'
$decade_array=explode('-',$decades);
$date_active=date("M-d-Y", mktime(0, 0, 0, 1,1 , trim($decade_array[0]) ));
$date_inactive=date("M-d-Y", mktime(0, 0, 0, 1, 1, trim($decade_array[1]) ));
echo $date_active.' '.$date_inactive;
해결책
alternative procedural function
date_format(date_create('1234-01-01'), 'M-d-Y');
다른 팁
Try with the DateTime
class:
$date = new DateTime("1234-01-01");
echo $date->format("M-d-Y"); // outputs Jan-01-1234
The DateTime class is available since PHP 5.2.
If you have PHP 5.3, use DateTime::createFromFormat
:
$date = DateTime::createFromFormat('Y-m-d', "1234-01-01");
echo $date->format("M-d-Y");
제휴하지 않습니다 StackOverflow