更改mySQL DATE函数的输出

时间:2012-12-07 06:16:35

标签: php html mysql database

我正在开发一个显示mySQL数据库日期的网站。我目前在数据库中有这些列:idtitledate_startdate_stoptime_starttime_stopimagehyperlink

我希望date_startdate_end显示为12月24日,而不是2012-12-24。

我目前的代码是:

<html>
<style>
html{
font-family: arial;
}
</style>
<h1>Local Events</h1>
<?php
//mysql connection
mysql_connect('localhost', 'root', '') OR DIE ("Unable to connect to database! Please try again later.");

//selecting the table
mysql_select_db('local');

//query
$query = mysql_query("SELECT * FROM events3 WHERE date_end >= CURDATE() ORDER BY date_start LIMIT 5;");

//http://stackoverflow.com/questions/2597098/get-the-last-n-rows-in-the-database-in-order
//SELECT * FROM events ORDER BY date_start DESC LIMIT 5 ::Works but still need to reverse order
//SELECT * FROM events ORDER BY id DESC LIMIT 5;
//SELECT * FROM events ORDER BY id DESC LIMIT 5;
//WHERE year = '2012'

//fetch results

        WHILE($rows= mysql_fetch_array($query)):

                $id =                   $rows['id'];   
                $title =                $rows['title'];

                $date_start =       $rows['date_start'];
                $date_end =             $rows['date_end'];

                $time_start =           $rows['time_start'];
                $time_end =             $rows['time_end'];

                $location =             $rows['location'];
                $description =          $rows['description'];
                $image =                $rows['image'];
                $hyperlink =            $rows['hyperlink'];                    
?>
<?php
        //ID/ Title Display
        echo "<b>ID:</b>&nbsp;&nbsp;&nbsp;";                                    echo "$id<br>";
        echo "<b>Title:</b>&nbsp;&nbsp;&nbsp;";                                 echo "$title<br>";

        //Date Display
        echo "<b>Date Start:</b>&nbsp;&nbsp;&nbsp;";                   
        echo "$date_start"; echo '&nbsp;-&nbsp';         echo "$date_end<br>";

        //Time Display
        echo "<b>Time Frame:</b>&nbsp;&nbsp;&nbsp;";                   
        echo date('g:i a',strtotime($time_start)); echo"&nbsp;-&nbsp;"; echo date('g:i a',strtotime($time_end)); echo"<br>";
        //      echo "$time_start";     echo "&nbsp;-&nbsp;";           echo "$time_end<br>";


        echo "<b>Location:</b>&nbsp;&nbsp;&nbsp;";              echo "$location<br>";
        echo "<b>Description:</b>&nbsp;&nbsp;&nbsp;";   echo "$description<br><br><br>";



        endwhile;
// SELECT * FROM events ORDER BY id DESC LIMIT 10;
?>

</html>

该代码使网站看起来像:

ID:   8
Title:   Christmas
Date Start:   2012-12-44 - 2012-12-25
Time Frame:   9:00 am - 1:00 pm
Location:   Everywhere
Description:   Test event

我希望日期开始区域可以说12月24日 - 25日。我该怎么做?

2 个答案:

答案 0 :(得分:0)

使用此查询

SELECT 
    id, title , 
    DATE_FORMAT(date_start, '%M/%d') as `Date Start`,
    other_columns
FROM
    mytable 

答案 1 :(得分:0)

SELECT CONCAT(DATE_FORMAT(date_start, '%M %d'), ' - ', DAY(date_end)) newDate
FROM table1

后续问题:如果你有这个日期怎么样? 2012-12-25 until 2013-02-15