按日期命令mysql查询

时间:2012-02-01 06:09:28

标签: php mysql

我使用以下代码显示从数据库中提取的事件详细信息。

<form name="event_form" id="event_form" action="" method="post"  enctype="application/x-www-form-urlencoded">
          <table width="765" border="0" align="left" cellpadding="0" cellspacing="0" >
          <tr>
          <td>
          <table width="765" border="0" align="left" cellpadding="0" cellspacing="0" id="results" class="fronttbl">
          <tr></tr>
          <?php
            $select = "SELECT * FROM `tbl_event`";
            $select_event = mysql_query($select);
            $select_num = mysql_num_rows($select_event);
            if($select_num > 0)
            {
                while($fetch = mysql_fetch_array($select_event))
                    {
                        $feventid=$fetch['intEventid'];
                        $feventname=stripslashes(ucfirst($fetch['varEventname']));
                        $fDate=$fetch['varDate'];
                        $seperate=explode("-", $fDate );
                        $year=$seperate[0];
                        $month=$seperate[1];
                        $date=$seperate[2];
                        $fchiefguest=stripslashes(nl2br($fetch['varChiefguest']));
                        $fvenue=stripslashes($fetch['varVenue']);
                        $ftime=stripslashes($fetch['varTime']);
                        $feventdetails=stripslashes($fetch['varEventdetails']);
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    ?>
                    <tr>
            <td>

            <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="30%" height="30" valign="top"><strong>Name of the event:</strong></td>
    <td width="70%" height="30" valign="top"><?php echo $feventname; ?></td>
  </tr>
  <tr>
    <td height="30" valign="top"><strong>Date of the event to be held:</strong></td>
    <td height="30" valign="top"><?php echo $date.'-'.$month.'-'.$year; ?></td>
  </tr>
  <tr>
    <td height="30" valign="top"><strong>Time of the Event:</strong></td>
    <td height="30" valign="top"><?php echo $ftime; ?></td>
  </tr>
  <tr>
    <td height="30" valign="top"><strong>Venue of the event:</strong></td>
    <td height="30" valign="top"><?php echo $fvenue; ?></td>
  </tr>
  <tr>
    <td height="30" valign="top"><strong>Name of the Chief Guest:</strong></td>
    <td height="30" valign="top"><?php echo $fchiefguest; ?></td>
  </tr>
   <tr>
    <td height="30" valign="top"><strong>Event Details:</strong></td>
    <td height="30" valign="top"><?php echo $feventdetails; ?></td>
  </tr>

</table>
<p style="border-bottom:1px dotted #CCCCCC;"></p>
            </td></tr>



    <?php               }

                }




          ?>
          </table>
          </td>
          </tr>
          </table>
          <div id="pageNavPosition"></div>
          </form>

我的数据库表中有日期字段,输入将保存为以下格式2012-03-01

我需要我的页面按前端的最近日期/月/年显示事件顺序。例如,今天的事件应首先同样显示。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

使用ORDER BY(......就是你的意思)?

将您的查询更改为:

SELECT *
FROM `tbl_event`
ORDER BY varDate DESC

答案 1 :(得分:0)

据我所知,这个功能用于前端:

function changeFormat( $date ){
   $exp_date = explode('-', $date);
   return $exp_date[2] . '-'.$exp_date[1].'-'.$exp_date[0];
}

示例:echo changeFormat('2012-12-01');将返回01-12-2012并在您想要显示日期的while循环中应用此内容