-1

I have some issues with some dates coming back from a query in the DD/MM/YYYY format and I'd like to make them appear in the MM/DD/YYYY format.

I had this:

."<td>".$row['date']."</td>\n"

And wanted to use this to format the date:

."<td>".date_format($row['date'],'m/d/y')."</td>\n"

But instead it seems to break it.

Is there any way to format the data the way I want it to appear?

UndefinedReference
  • 1,223
  • 4
  • 22
  • 52
  • 4
    php's date functions expect a unix timestamp, or a datetime object. you can't just stuff in arbitrary string into them and expect things to work. you'd either need to format the date in your DB (of which you've provided no details), or use something like php's `strtotime()` to convert the from-db version into a timestamp which CAN be fed to php's `date()` function. – Marc B Jun 02 '14 at 22:14
  • `date('m/d/Y',strtotime($row['date']));` but please, don't ever use the worst date formatting ever to be invented on this earth – Félix Adriyel Gagnon-Grenier Jun 02 '14 at 22:18
  • try with `DateTime::createFromFormat('d/m/Y', $row['date'])->format('m/d/y');` – Thiago França Jun 02 '14 at 22:20
  • try: `date('m/d/Y', strtotime(str_replace('/','-',$row['date'])));` – Cyclonecode Jun 02 '14 at 22:26

2 Answers2

0
Try this:
    $formatted_date= date('m/d/Y',$row['date']);

    "<td>".$formatted_date."</td>";
akr
  • 739
  • 4
  • 15
0

Assuming your query is in PHP do this:

<?
$originalDate = $row['date'];
$newDate = date("m/d/Y", strtotime($originalDate));
echo $newDate;
?>
NCoder
  • 325
  • 3
  • 10
  • 25