2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel='stylesheet' type='text/css' href='style.css'/>
<title>Files</title>
</head>

<body class="body">
<?php
$prodnum = $_GET['prodnum'];

            $con=mysqli_connect("localhost","root","","sp");
            if (mysqli_connect_errno($con))
            {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }
                $res = mysqli_query($con,"SELECT * FROM product_info WHERE prodnum = '$prodnum'") OR die(mysqli_error($con)) ;  


                            echo '
                                            <table id="table">
                                            <tr>
                                            <td> <h3> Product Information </h3> </td>
                                            </tr>';


                                while($row = mysqli_fetch_array($res)){
                                            $prodnum=$row[0];
                                            $farm=$row[1];
                                            $sh=$row[2];
                                            $sdate=$row[3];
                                            $pdate=$row[4];
                                            $edate=$row[5];
                                            $cert=$row[6];
                                            $shsite=$row[9];
                                            $farmsite=$row[10];


                                            echo'<tr>
                                            <td> <b> Product Number:</b> '.$prodnum.'
                                            </td>
                                            </tr>
                                            <tr>
                                            <td> <b> Farm:  </b> <a class="links" href='.$farmsite.'> '.$farm.' </a>
                                            </td>
                                            </tr>
                                            <tr>
                                            <td> <b> Slaughter House: </b> <a class="links" href='.$shsite.'> '.$sh.' </a>
                                            </td>
                                            </tr>
                                            <td> <b> Date Slaughtered: </b>'.$sdate.' 
                                            </td>
                                            </tr>
                                            <tr>
                                            <td><b> Date Produced: </b>'.$pdate.' 
                                            </td>
                                            </tr>
                                            <tr>
                                            <td> <b> Expiration Date: </b> '.$edate.' 
                                            </td>
                                            </tr>
                                            <tr>
                                            <td> <b> Certified by: </b> '.$cert.' 
                                            </td>
                                            </tr>
                                            </table>'
                                            ;
                                }


?>
</body>
</html>

This code displays the data from my database. I do not how to display the date in mysql in human readable format. The result of this code is for example 2014-03-04 but I want to display it like March 04, 2013. The data type of the dates are date in the mysql database. I only see converting the timestamp data type. How can I do it? How can I also put it in the and tag? Thank you.

duderbear
  • 101
  • 3
  • 16
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 29 '14 at 08:36

4 Answers4

1

Try this function

date('F d, Y',strtotime($your_date_variable));

For more information check here

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
1
echo date("F d, y",strtotime($pdate));

Use php date function to format any date time, and use php strtotime function to convert any string to date. It returns "March 29, 2014"

  • Nice, you took the time to construct the format, the OP asks for. Links to reference manuals increase the educative value of your answer. – Chris Wesseling Mar 29 '14 at 08:44
1

Before you continue, look at the comment on SQL injection

You can let MySQL do it for you with the date_format function. Instead of selecting * do something like:

SELECT DATE_FORMAT(name_of_datefield, '%M %d, %Y') as slaughterdate, ... FROM ...

Or you can do it on the PHP side by using the date function:

date('F d, Y', $sdate);

So for your code you could change:

$sdate=$row[3];
$pdate=$row[4];
$edate=$row[5];

into

$sdate = date('F d, Y', $row[3]);
$pdate = date('F d, Y', $row[4]);
$edate = date('F d, Y', $row[5]);
Chris Wesseling
  • 6,226
  • 2
  • 36
  • 72
0

Why not use DateTime

$date = new DateTime('2000-01-01');
echo $date->format('jS F Y');

So in your case u have to do as

$date = new DateTime($pdate);
echo $date->format('jS F Y');
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63