-4

please i need help in converting string to date

my string is $search = '6/2/2014'

i used this way but it doesn't work correctly

$time = strtotime('$search');

$newformat = date('DD-MM-YYYY',$time);

echo $newformat;

the result is displayed as 01-01-70 What should I do for this output help.

i'm PosSible
  • 1,373
  • 2
  • 11
  • 30

1 Answers1

1

First of all DON'T use strtotime as it is unreliable. Use the new DateTime class. In your case use something like this:

$time = DateTime::createFromFormat('m/d/Y', $search, new DateTimeZone('America/New_York'));
$newformat = $time->format('d-m-Y');
echo $newformat;
Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28