-2

I tried converting

12-18-1997

to

18-12-1997

with this code

$new_date = date('d-m-Y', strtotime('12-18-1997'));

but it results in 18-12-1969

If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.

Muhammad Imran Tariq
  • 21,578
  • 43
  • 119
  • 188
  • Please dont mark it duplicate. If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time. – Muhammad Imran Tariq Jul 30 '13 at 18:18

2 Answers2

0

Use DateTime instead of strtotime():

$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');

You can see from this demo that it prints:

18-12-1997
nickb
  • 58,150
  • 12
  • 100
  • 138
0

strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:

php > var_dump(strtotime('12-18-1997'));
bool(false)

Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.

If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:

$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);
Marc B
  • 348,685
  • 41
  • 398
  • 480