1

I have a database full of football fixtures, sorted in descending order. Everything works fine however the dates are in the following format: YYYYMMDD like so - 2014-05-11.

All table contents are selected with:

 $result = mysqli_query($con,"SELECT * FROM Fixtures ORDER BY Date DESC");

Then inserted into the table with the below code:

while($row = mysqli_fetch_array($result))
  {

  echo "<tr>";
  echo "<td>" . $row['Date'] . "</td>";
  echo "<td>" . $row['Home Team'] . "</td>";
  echo "<td>" . $row['Score'] . "</td>";
  echo "<td>" . $row['Away Team'] . "</td>";
  echo "<td>" . $row['Score1'] . "</td>";
  echo "<td>" . $row['Competition'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

How can I make it so these dates appear DDMMYYYY such as 11-05-2014?

Thanks!

2

3 Answers 3

4

Solution:

echo "<td>" . date('d-m-Y', strtotime($row['Date'])) . "</td>";

Manual

Sign up to request clarification or add additional context in comments.

2 Comments

that'd only work if mysql actually returns a unix timestamp. date('d-m-Y', '2014-04-28') isn't going to work at all.
Hi, Just tried this method and all dates are now set as: 31-12-1969??
1

Use DateTime::createFromFormat

$date = DateTime::createFromFormat('dmY', $row['Date']);
echo $date->format('d-m-Y');

2 Comments

without sounding like an idiot how would I put this into my code?
DateTime::createFromFormat will accept the date from a specified form. Like in your case its dmY, then $date->format('d-m-Y') will hold the format as you want. DateTime is a powerful class in PHP to handle datetime conversion and should be used intead of other conversion technique which are not always correct. You can add the first line inside the loop and then echo "<td>" . $date . "</td>"; thats all.
1

Either fetch it properly from the database:

$query = "SELECT
    ...,
    ...,
    DATE_FORMAT(Date, '%d-%m-%Y') AS Date
FROM Fixtures
ORDER BY Date DESC";

$result = mysqli_query($con, $sql);

Or format it in your PHP

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

1 Comment

Am i doing something wrong with my method? I don't have much knowledge in PHP so thought this would be fine. Without sounding too much like a noob, how would i implement your above code into mine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.