0

我正在一个显示来自 mySQL 数据库的日期的网站上工作。我目前在数据库中有这些列:id, title, date_start, date_stop, time_start, time_stop, image, 和hyperlink.

我希望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 日。我该怎么做?

4

2 回答 2

0

使用此查询

SELECT 
    id, title , 
    DATE_FORMAT(date_start, '%M/%d') as `Date Start`,
    other_columns
FROM
    mytable 
于 2012-12-07T06:21:21.243 回答
0
SELECT CONCAT(DATE_FORMAT(date_start, '%M %d'), ' - ', DAY(date_end)) newDate
FROM table1

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

于 2012-12-07T06:27:11.363 回答