2

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;
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
Devin Dixon
  • 11,553
  • 24
  • 86
  • 167

2 Answers2

7

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");
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
2

alternative procedural function

date_format(date_create('1234-01-01'), 'M-d-Y');
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • 1
    ([`date_create(...)`](http://php.net/manual/en/datetime.construct.php) is an alias for [`new DateTime(...)`](http://php.net/manual/en/datetime.construct.php) and returns a `DateTime` object, both are available since PHP 5.2. `date_format` is an alias for `DateTime->format()` and takes a `DateTime` object as parameter.) – Arnaud Le Blanc Jan 30 '11 at 21:07