-1

I have a date in my column that is varchar(30) and in my table it appears the format d/m/y I want to convert it to m/d/y

I try this but wont work. please help.

$test = new DateTime('date');
echo '<td>' .date_format($test, 'm-d-Y').$row.'</td>';
  • First mistake: Storing a date as a varchar. You should have stored it as an actual date/datetime/timestamp value instead, and then your question would be trivial. – Marc B Sep 08 '14 at 16:59
  • I tried to stored it as a date but the output becomes **0000-00-00** – user3896397 Sep 08 '14 at 17:00
  • mysql date strings must be in `yyyy-mm-dd hh:mm:ss` format. you can't stuff in any random string you want and expect mysql to figure out what it is.e.g. what's `01/02/03`? Jan 2nd, 2003? March 1st, 2002? – Marc B Sep 08 '14 at 17:09
  • I've already solved the problem and thanks to user4013047. – user3896397 Sep 08 '14 at 17:23

2 Answers2

2

You can use DateTime::createFromFormat and DateTime::format:

$date = DateTime::createFromFormat('d/m/y', $row['date']);
echo $date->format('m/d/y');
dave
  • 62,300
  • 5
  • 72
  • 93
0

you can use:

echo date("m-d-Y",strtotime($row['date']));

example:

echo date("m-d-Y",strtotime("8/7/2014"));

where $row['date'] is date fetched from db table.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Pulkit
  • 137
  • 5