Mucca019
Mucca019

Reputation: 201

Changing the way date is displayed in php table

When adding data it is entered in the form of dd-mm-yyyy, however in the database it then appears as yyyy-mm-dd. This isn't an issue, however when pulling that data back onto my website i want the date to be displayed in the dd-mm-yyyy format. Im displaying the data in a datatable. Code I am currently using is:

while ($myrow = mysqli_fetch_array($result)):

$username   = $myrow["username"];
$date       = $myrow["date"];
$returned   = $myrow["returned"];

$table = '<tr>';

if ($returned == 'Yes'){ 
        $table.= '<td><div style="color: green;">'.$username.'</div>';
    } else if ($date < date("Y-m-d")) {
        $table.= '<td><div style="color: red;">'.$username.'</div>';
    } else{
        $table.= '<td><div style="color: blue;">'.$username.'</div>';
    }

if ($returned == 'Yes'){ 
        $table.= '<td><div style="color: green;">'.$date.'</div>';
    } else if ($date < date("Y-m-d")) {
        $table.= '<td><div style="color: red;">'.$date.'</div>';
    } else{
        $table.= '<td><div style="color: blue;">'.$date.'</div>';
    }

 if ($returned == 'Yes'){ 
        $table.= '<td><div style="color: green;">'.$returned.'</div>';
    } else if ($date < date("Y-m-d")) {
        $table.= '<td><div style="color: red;">'.$returned.'</div>';
    } else{
        $table.= '<td><div style="color: blue;">'.$returned.'</div>';
    }

echo $table;
endwhile;

All data being brought through from the database is working perfectly etc, just curious as to what im missing to change the date format.

Any help is greatly appreciated.

Upvotes: 1

Views: 2926

Answers (6)

Murillio4
Murillio4

Reputation: 495

 while ($myrow = mysqli_fetch_array($result)):

    $username   = $myrow["username"];
    $date       = $myrow["date"];
    $returned   = $myrow["returned"];
    $color;

    $formattedDate = date("d-m-Y", strtotime($myrow["date"]));

    $table = '<tr>';

    if ($returned == 'Yes'){ 
            $color = "green";
        } else if ($date < date("Y-m-d")) {
            $color = "red";
        } else{
            $color = "blue";
        }


       $table.= '<td><div style="color: '.$color.';">'.$username.'</div></td>';
       $table.= '<td><div style="color: '.$color.';">'.$formattedDate.'</div></td>'
       $table.= '<td><div style="color: '.$color.';">'.$returned.'</div></td>';


    $table .= '<tr>';

    echo $table;
 endwhile;

Upvotes: 1

Paras Pitroda
Paras Pitroda

Reputation: 208

If you don't want to change in PHP, you can manage it with MySQL query too. Update your query with:

Select username,DATE_FORMAT(date,'%m-%d-%Y') as date,returned from TABLE.....

Upvotes: 0

Tyler S. Smith
Tyler S. Smith

Reputation: 46

Straight from the PHP documentation.

d - Day of the month, 2 digits with leading zeros - 01 to 31
m - Numeric representation of a month, with leading zeros - 01 through 12
Y - A full numeric representation of a year, 4 digits - 1999 or 2003

So you might do something like this for what you are looking to do.

$time = strtotime($timefromdb);
$formatted = date("d/m/Y", $time);

Going through the documentation will give you a very wide range of options when it comes to this sort of thing.

Upvotes: 0

clearshot66
clearshot66

Reputation: 2302

Format the date:

http://php.net/manual/en/function.date.php

$d = new DateTime($date);
$formatted= date_format($d, 'Y m d');  

Upvotes: 0

jrn
jrn

Reputation: 2800

You can use the following to change the date format:

$formattedDate = date("d-m-Y", strtotime($myrow["date"]));

Instead of using your $date variable, use $formattedDate :-)

Upvotes: 1

Rohit
Rohit

Reputation: 3146

Well, you have to actually change the date format.

The simplest way is to convert the date to a timestamp then use the date function to display the date in whatever format you want. You can use the strtotime function to turn it into a timestamp, and date has lots of display options.

Upvotes: 0

Related Questions