1

I am using Codeigniter i have this code within a view, however the format of the date within the database is YYYY-MM-DD i am wanting to changing it to DD-MM-YYYY. I am unable to change it within phpMyAdmin so i am looking for a solution in php.

"Date" is a column within my database just to cause confusion.

$query = $this->db->query('SELECT Date, Home, HomeScore, AwayScore, Away FROM Results');

        foreach ($query->result_array() as $row)
        {
            echo '<div class="col-md-4 col-sm-12"><h3>';
            echo $row['Date'];
            echo "</h3><h4>Premier League</h4><h5>";
            echo $row['Home'];
            echo "</h5><span>";
            echo $row['HomeScore'];
            echo "</span><span>";
            echo $row['AwayScore'];
            echo "</span><h5>";
            echo $row['Away'];
            echo "</h5></div>";
        }
1
  • Avoid using reserved word as column name. Commented Jan 24, 2017 at 11:21

3 Answers 3

1
$date = $row['Date'];


echo date("d/m/Y",strtotime($date));
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thank you.
0

First off, love the picture. I'm a fan of Darth Vader as well.

Second, try this:

$date = $row['Date'];

echo date("D/M/Y",strtotime($date));

Edit: It appears someone already showed you this. My bad.

Hope it helps, and in relation to your picture...Live long and prosper.

1 Comment

Thanks anyway! May the force be with you too.
0

Try like this.The DATE_FORMAT() function is used to display date/time data in different formats.

SELECT DATE_FORMAT(Date,'%d-%m-%Y'), Home, HomeScore, AwayScore, Away FROM Results

For more see here http://www.w3schools.com/sql/func_date_format.asp

3 Comments

I added this in refreshed and i got an error "unexpected '%'" so I removed the '%' and received an new error "unexpected 'd' (T_STRING)".
It's not working for me, I have tried many different ways using this method however i did encounter a SQL error. I have deiced to use $date = $row['Date']; echo date("d/m/Y",strtotime($date));
DATE_FORMAT(Date,'%d-%m-%Y')... Date without quotes

Your Answer

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