简体   繁体   中英

date in php human readable format

<!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.

Try this function

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

For more information check here

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"

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]);

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM