简体   繁体   中英

How to get the data from database on current date automatic

i want to get the data from database on current date. when i open page then i see get the data of today current date. if i select the another date then i get the another date data from database here is live example of my concept http://joomusic.info/joosilver.php if you visit here then you understand my concept

<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<script src="jquery.js"></script>
 <script>
   $(document).ready(function()
 {
   //ajaxTime.php is called every second to get time from server
   var refreshId = setInterval(function()
 {
   $('#timeval').load('ajaxTime.php?randval='+ Math.random());
 }, 1000);

   //stop the clock when this button is clicked
   $("#stop").click(function()
 {
   clearInterval(refreshId);
 });
 });
</script>
<form action="ko.php" method="get">
From : <input type="text" name="d1" class="tcal" value="<?php echo date("m/d/Y"); ?>" /> 
<input type="submit" value="SHOW">
</form>

<table id="resultTable" data-responsive="table" style="text-align: center; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
<tr>
            <tr  bgcolor="#F1EDC2">
    <td><font color='#2F4F4F'><h2> Draw Time</h2></font></td>
    <td><font color='#2F4F4F'><h2> Wining Number</h2></font></td>
</tr>
</thead>
<tbody>
  <?php
    include('connect.php');
    if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=('Y-m-d H:i:s'); }; 
    $result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
    $result->bindParam(':a', $d1);
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
  ?>
<tr class="record">
            <tr  bgcolor="#EEF3E2">
    <td><font size=5><font color='#008B00'><?php echo $row['dt']; ?></font></td>
    <td><font size=5><font color='#008B00'><?php echo $row['wn']; ?></font></td>
</tr>
<?php
}
?>
</tbody>
</table>

if i change this line $result = $db->prepare("SELECT * FROM birthday WHERE date = :a"); into $result = $db->prepare("SELECT * FROM birthday WHERE date <= :a"); then i get all dates data ... but i want only current date data automatic here is live example of my concept http://joomusic.info/joosilver.php i want like this 100%

three points to consider: 1. if you just want the current date result then just use "=". 2.here we need to see what is the datatype of you date column in the birthday table. if the datatype is datetime then add hh:mm:ss to the date, or if the datatype is just date then just use date. 3. if the date column is int and storing them in timestamp then convert date to microtime and use it.

SELECT * FROM yourTable WHERE CURDATE() >= STR_TO_DATE( date , '%m-%d-%Y') ORDER BY STR_TO_DATE( date , '%m-%d-%Y') DESC LIMIT 1

You didn't mention the data type of your date column.

But date, timestamp, and datetime items are just a little tricky to use in WHERE statements. If you want to find all the rows on a particular date the best way to do it is with:

WHERE whatever
  AND `date` >= DATE(:a)
  AND `date` <  DATE(:a) + INTERVAL 1 DAY

This gets all the date values starting at midnight on the day I mentioned, up until but not including midnight on the day after the day I mentioned.

You could also do this (or some equivalent)

WHERE whatever
  AND DATE(`date`) = DATE(:a)  /* slow! */

but it will prevent the use of an index to satisfy your query, making it slow.

Here's an essay I wrote offering some background. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

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