在SQL查询上更改日期格式

时间:2012-12-29 15:04:12

标签: php mysql sql

我将一些变量通过表单(月份和年份)传递到SQL查询中,该查询根据选定的月份和年份显示结果。我有一个月份和年份的选择菜单。我的日期时间戳是(yyyy,mm,dd)当我回显结果而不是数值时,如何显示月份的全文?这是我的查询

        // Query For Contract Items
    if(isset($_GET['month'])){
    $month = $_GET['month'];
    $year = $_GET['year'];
    $ds = (int)$year."-". (int)$month;
    $result = mysql_query("SELECT * FROM projects WHERE  end_date LIKE '$ds%' AND status!='Invoiced' AND status!='Done' AND status!='Delayed' AND billed='Contract' ORDER BY client, start_date DESC");

$result2 = mysql_query("SELECT SUM(amount) * 100 AS sum_ammount FROM projects WHERE end_date LIKE '$ds%' AND status!='Invoiced' AND status!='Done' AND status!='Delayed' AND billed='Contract' ORDER BY client, start_date DESC");
$information = mysql_fetch_array($result2);
}


<form action="est.php" method="get">
                          <table width="920" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td align="left" valign="top">
                                  <fieldset style="float:left; margin-right:20px;">
                                   <label>Month</label>
                                    <select name="month">
                                    <option value='01' >January</option>
                                    <option value='02'>February</option>
                                    <option value='03'>March</option>
                                    <option value='04'>April</option>
                                    <option value='05'>May</option>
                                    <option value='06'>June</option>
                                    <option value='07'>July</option>
                                    <option value='08'>August</option>
                                    <option value='09'>September</option>
                                    <option value='10'>October</option>
                                    <option value='11'>November</option>
                                    <option value='12'>December</option>
                                  </select>
                                  </fieldset>
                                    <fieldset style="float:left; margin-right:20px;">
                                   <label>Year</label>
                                   <input type=text name="year" size="4" style="width:100px;" value="<?php echo $year; ?>"></fieldset>
                                  <fieldset style="float:left; margin-right:20px;">
                                  <input type="submit" style="margin-top:16px;" value="Crunch Numbers">
                                  </fieldset>
      </td></tr></table>
      </form>
  <div style="clear:both"></div>


<h3><?php echo $month; ?> / <?php echo $year; ?> Contract</h3>
<table id="myTable" class="tablesorter" cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th width="10%">Client</th>

                    <th width="10%">Project</th>

                    <th width="10%">Designer</th>

                    <th width="5%">Start Date</th>

                    <th width="5%">End Date</th>

                    <th width="10%">Time</th>

                    <th width="10%"></th>
                </tr>
            </thead>

            <tbody>



            <?php
                            while ($row = mysql_fetch_assoc($result)) {

                                                                $id = $row['id'];
                                                                $client = $row['client'];
                                                                $title = $row['title'];
                                                                $assigned = $row['assigned'];
                                                                $start_date = date('F d, Y', strtotime($row['start_date']));
                                                                $end_date = date('F d, Y', strtotime($row['end_date']));
                                                                $status = $row['status'];
                                                                $amount = $row['amount'];
                                                                $billed = $row['billed'];

                                                        ?>



                            <?php 

                            $x++; 

                            $class = ($x%2 == 0)? 'whiteBackground': 'graybackground';

                            echo "<tr class=\"$class\">";

                            ?>


                    <td width="10%"><strong><span class="sentanceCase"><?php echo $client; ?></span></strong></td>

                    <td width="10%"><?php echo $title; ?><br/>

                   <span class="gray"><?php echo $billed; ?></span>

                   <span class="hours">...............................</span><br />
                   <?php echo $status; ?>

                   </td>

                    <td width="5%"><?php echo $assigned; ?></td>

                    <td width="10%"><span class="hours"><?php echo $start_date; ?></span></td>

                    <td width="10%">

                    <?
                    if (strtotime($row['end_date']) <= strtotime(date('Y-m-d'))) {

                        echo ' <span style="color:red;">';
                        echo $end_date;
                        echo ' <span>';
                    }
                    else{
                         echo $end_date;
                    }

                    ?>



                  </td>

                    <td width="10%">


                    <?php echo $amount; ?> Est. Hrs<br />

                    <span class="hours">...............................</span> <br />


                    <?php
                    if ( $feeA >= 1 )
                    {
                        echo $feeA;
                        echo ' Mang/Coord <br/>';
                    }

                    ?> <?php
                    if ( $feeB >= 1 )
                    {
                        echo $feeB;
                        echo ' Coord/Res <br/>';
                    }

                    ?> <?php
                    if ( $feeC >= 1 )
                    {
                        echo $feeC;
                        echo ' Design & Dev <br/>';
                    }

                    ?> <?php
                    if ( $feeD >= 1 )
                    {
                        echo $feeD;
                        echo ' Art D <br/>';
                    }

                    ?> <?php
                    if ( $feeE >= 1 )
                    {
                        echo $feeE;
                        echo ' Web Design <br/>';
                    }

                    ?> <?php
                    if ( $feeF >= 1 )
                    {
                        echo $feeF;
                        echo ' Copy <br/>';
                    }

                    ?> <?php
                    if ( $feeG >= 1 )
                    {
                        echo $feeG;
                        echo ' Photography <br/>';
                    }

                    ?> <?php
                    if ( $feeH >= 1 )
                    {
                        echo $feeH;
                        echo ' Enviro Design <br/>';
                    }

                    ?> <?php
                    if ( $feeI >= 1 )
                    {
                        echo $feeI;
                        echo ' Photo Manip <br/>';
                    }

                    ?> <?php
                    if ( $feeK >= 1 )
                    {
                        echo $feeK;
                        echo ' Marketing <br/>';
                    }

                    ?> <?php
                    if ( $feeL >= 1 )
                    {
                        echo $feeL;
                        echo ' Strategy Dev <br/>';
                    }

                    ?>
                    <?php
                    if ( $feeM >= 1 )
                    {
                        echo $feeM;
                        echo ' Web Level 1 <br/>';
                    }

                    ?>

                     <?php
                    if ( $feeN >= 1 )
                    {
                        echo $feeN;
                        echo ' Web Level 2 <br/>';
                    }

                    ?>

                     <span class="hours">...............................</span> <br />

                    <?php
                    if ( $billing_need == 'Yes' )
                    {
                        echo '<span style="color:red">';
                        echo $extended_billing;
                        echo '<span>';
                    }

                    ?>


                    </td>


                    <td width="10%">
                    <div id="changes">

                    <div id="edit"><a id="editBTN" href="edit.php?id=<?php echo $id; ?>" alt="Edit Project" title="Edit Project" ></a></div>
                    <div id="trash"><a id="trashBTN" href="delete.php?id=<?php echo $id; ?>" alt="Trash Project" title="Trash Project" ></a></div>

                    </div>
                    </td>
                </tr><?php
                                                        }

                                                        ?>
            </tbody>

            <tfoot>
                <tr>
                    <th width="10%">Client</th>

                    <th width="10%">Project</th>

                    <th width="5%">Designer</th>

                    <th width="10%">Start Date</th>

                    <th width="10%">End Date</th>

                    <th width="10%">Times</th>


                    <th width="10%"></th>
                </tr>
            </tfoot>
        </table>
        <?php echo $information['sum_ammount']; ?>

3 个答案:

答案 0 :(得分:0)

您可以使用date()函数来实现此目的。

date('F', [$timestamp]);

将显示月份。如果省略$ timestamp,则使用当前时间戳。在您的示例中,您将必须准备时间戳。您可以使用strtotime()

答案 1 :(得分:0)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

SELECT SUM(amount) * 100 AS sum_ammount, DATE_FORMAT(end_date, '%Y, %b') AS date FROM projects WHERE end_date LIKE '$ds%' AND status!='Invoiced' AND status!='Done' ORDER BY end_date DESC

答案 2 :(得分:0)

假设我理解你的意思,你应该能够在SQL查询中执行以下操作

select MONTHNAME(end_date), sum(amount)*100 as sum_ammount from projects where ...

应返回月份名称。