繁体   English   中英

如何在PHP中以DMY格式按Mysql数据库中的日期列显示并基于它进行搜索

[英]How Can I Display By Date Column From Mysql Database In D-M-Y Format in PHP And Make Search Based on it

在这里,我有我的 PHP 代码。 我想要做的是,我想在我的 php 页面中以 DMY 格式显示数据库中的日期。 我也想通过提供日期作为输入来进行搜索。 你们可以告诉我我该怎么做吗?

<?php
$connect = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("store_records",$connect) or die(mysql_error());

$sql = "SELECT * FROM distributor_records";

if (isset($_POST['submit'])) {
$search_term = mysql_real_escape_string($_POST['search_box']); 

$sql = "SELECT * FROM distributor_records WHERE Distributor_name LIKE '%{$search_term}%' ORDER BY Payment_Date DESC";

}

$query = mysql_query($sql) or die(mysql_error());

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<link href="Style.css" rel="stylesheet" type="text/css"/>
<title>Search Results</title>
</head>
<body bgcolor="#FFFFFF">
<div id="holder">
<div id="header">
<div id="header-contents">
<center>
<img src="images/icon.png" height="89px"/><br/>
<b>DRUG STORE RECORDS MANAGEMENT SYSTEM</b>
</center>
</div>
</div>

<div id="marquee">
<marquee align="left" behavior="scroll" bgcolor="#FFFF00"><b><big>ANGKITA DRUG STORE</big></b> </marquee>
</div>

<div id="Navbar">
<nav>
 <ul>
 <li><div style="width:121px; display:block"><a href="index.html">HOME</a></div> </li>
 <li><div style="width:170px; display:block"><a href="#">DUE PAYMENTS</a></div></li>
 <li><div style="width:190px; display:block"><a href="Purchased Items.php">PURCHASED ITEMS</a></div></li>
 <li><div style="width:239px; display:block"><a href="Distributor Payments.php">DISTRIBUTOR PAYMENTS</a></div></li>
 <li><div style="width:170px; display:block"><a href="expenditurerecords.php">EXPENDITURES</a></div></li>
</ul>
</nav>
</div>

<div id="content-search">
<table align='center' border='2'>
<tr>
<th bgcolor=#ddd colspan=8>Searh Results For Distributor Records</th></tr>
<tr>
<th bgcolor=#ddd>Date</th>
<th bgcolor=#ddd>Name</th>
<th bgcolor=#ddd>Mobile No</th>
<th bgcolor=#ddd>Email</th>
<th bgcolor=#ddd>Payment</th>
<th bgcolor=#ddd>Address1</th>
</tr>

<?php while ($row = mysql_fetch_array($query)) {?>
<tr>
<td bgcolor=white><?php echo $row ['Payment_Date'];  ?></td>
<td bgcolor=white><?php echo $row ['Distributor_name'];  ?></td>
<td bgcolor=white><?php echo $row ['Mobile_No'] ; ?></td>
<td bgcolor=white><?php echo $row ['Email']; ?></td>
<td bgcolor=white><?php echo $row ['Payment']; ?></td>
<td bgcolor=white><?php echo $row ['Address1']; ?></td>
</tr>
<?php }?>

</table>
</div>

<div id="footer">
<div id="footer-content">&nbsp;&nbsp;Developed By: JYOTISHMOY BORAH<br/>
<div style="border-bottom:2px solid #FFFFFF; width:280px; margin-bottom:5px"></div>
<div style="margin-left:94px"><a href="#"> <img src="images/download.png" height="35px"></a>&nbsp;&nbsp;&nbsp;<a href="#"><img src="images/download.jpg" height="35px"></a></div>

</div>
</div>

</div>
</body>
</html>

这是从表中获取的数据的屏幕截图...从mysql中获取的表数据的图像

<?php
$connect = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("store_records",$connect) or die(mysql_error());

$sql = "SELECT * FROM distributor_records";

if (isset($_POST['submit'])) {
$search_term = mysql_real_escape_string($_POST['search_box']);
$condition = " date_format(Payment_Date, '%Y-%m-%d') LIKE %'.$search_term.'%";


$sql = "SELECT * FROM distributor_records WHERE 1 AND $condition ORDER BY Payment_Date DESC";

}

$query = mysql_query($sql) or die(mysql_error());

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<link href="Style.css" rel="stylesheet" type="text/css"/>
<title>Search Results</title>
</head>
<body bgcolor="#FFFFFF">
<div id="holder">
<div id="header">
<div id="header-contents">
<center>
<img src="images/icon.png" height="89px"/><br/>
<b>DRUG STORE RECORDS MANAGEMENT SYSTEM</b>
</center>
</div>
</div>

<div id="marquee">
<marquee align="left" behavior="scroll" bgcolor="#FFFF00"><b><big>ANGKITA DRUG STORE</big></b> </marquee>
</div>

<div id="Navbar">
<nav>
 <ul>
 <li><div style="width:121px; display:block"><a href="index.html">HOME</a></div> </li>
 <li><div style="width:170px; display:block"><a href="#">DUE PAYMENTS</a></div></li>
 <li><div style="width:190px; display:block"><a href="Purchased Items.php">PURCHASED ITEMS</a></div></li>
 <li><div style="width:239px; display:block"><a href="Distributor Payments.php">DISTRIBUTOR PAYMENTS</a></div></li>
 <li><div style="width:170px; display:block"><a href="expenditurerecords.php">EXPENDITURES</a></div></li>
</ul>
</nav>
</div>

<div id="content-search">
<table align='center' border='2'>
<tr>
<th bgcolor=#ddd colspan=8>Searh Results For Distributor Records</th></tr>
<tr>
<th bgcolor=#ddd>Date</th>
<th bgcolor=#ddd>Name</th>
<th bgcolor=#ddd>Mobile No</th>
<th bgcolor=#ddd>Email</th>
<th bgcolor=#ddd>Payment</th>
<th bgcolor=#ddd>Address1</th>
</tr>

<?php while ($row = mysql_fetch_array($query)) {?>
<tr>
<td bgcolor=white><?php echo $row ['Payment_Date'];  ?></td>
<td bgcolor=white><?php echo $row ['Distributor_name'];  ?></td>
<td bgcolor=white><?php echo $row ['Mobile_No'] ; ?></td>
<td bgcolor=white><?php echo $row ['Email']; ?></td>
<td bgcolor=white><?php echo $row ['Payment']; ?></td>
<td bgcolor=white><?php echo $row ['Address1']; ?></td>
</tr>
<?php }?>

</table>
</div>

<div id="footer">
<div id="footer-content">&nbsp;&nbsp;Developed By: JYOTISHMOY BORAH<br/>
<div style="border-bottom:2px solid #FFFFFF; width:280px; margin-bottom:5px"></div>
<div style="margin-left:94px"><a href="#"> <img src="images/download.png" height="35px"></a>&nbsp;&nbsp;&nbsp;<a href="#"><img src="images/download.jpg" height="35px"></a></div>

</div>
</div>

</div>
</body>
</html>

在这种情况下,您可以使用DATE_FORMAT

$sql = "SELECT DATE_FORMAT(Payment_Date, '%d-%m-%Y') as Payment_Date, Distributor_name, Mobile_No, Email, Payment, Address1 FROM `distributor_records`";

if (isset($_POST['submit'])) {
     $search_term = mysql_real_escape_string($_POST['search_box']);
     $sql = "SELECT DATE_FORMAT(Payment_Date, '%d-%m-%Y') as Payment_Date, Distributor_name, Mobile_No, Email, Payment, Address1 FROM distributor_records WHERE DATE_FORMAT(Payment_Date, '%Y-%m-%d') LIKE %'".$search_term."'% ORDER BY Payment_Date DESC";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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