3

我无法找到或弄清楚如何将 MySQL 日期字段输出为缩写的星期几。

我不确定是否在 MySQL 或 PHP 中执行此操作。

理想情况下,我想使用 PHP 函数来执行此操作,但更复杂的是,我想作为星期几输出的日期字段正在通过 while 循环运行到表中。这让我觉得我需要在 MySQL 中做这件事。

基本上,我希望能够使用 PHP 的 mktime() 或 MySQL 的 DAYOFWEEK() 之类的东西,除非因为我需要能够在 while 循环中执行此操作,所以我需要能够在功能,而不必输入或提取一个特定的日期进行格式化。

任何帮助是极大的赞赏!

PS 我在下面添加了数据当前来自数据库的方式。### 是我遇到麻烦的地方;这是我需要使用“e_date”列来回显这一天的地方。(下一个日期也使用“e_date”列。)

// Get all the data from the "events" table
            $result = mysql_query("
            SELECT *
            FROM events
            WHERE e_type != '' && e_date >= CURDATE() && e_date <= (CURDATE() + 15)
            ORDER BY e_date,e_ampm,e_time") 
            or die(mysql_error());  

            echo "<table border='1' style=\"border:none;font-size:12px;\">";
            echo "<tr> <th>Day</th> <th>Date</th> <th>Time</th> <th>Type</th> <th>Description</th> </tr>";
            // keeps getting the next row until there are no more to get
            while($row = mysql_fetch_array( $result )) {
                // Print out the contents of each row into a table
                echo "<tr><td>";
                ###
                echo "</td><td>";
                echo $row['e_date'];
                echo "</td><td>";
                echo $row['e_time'];
                echo $row['e_ampm'];
                echo "</td><td>";
                echo $row['e_type'];
                echo "</td><td>";
                echo $row['e_name'];
                echo "</td></tr>"; 
            } 

            echo "</table>";
4

3 回答 3

2

试试这个修改后的版本。您可以使用 DATE_FORMAT,使用日期字段作为输入变量,%a 表示输出的格式。%a 告诉 DATE_FORMAT 返回日期的缩写日期。试一试,看看它是否有效,我有一段时间没有使用 MySQL,所以我可能是错的。

// Get all the data from the "events" table
        $result = mysql_query("
        SELECT DATE_FORMAT(e_date, '%a') as day, e_date, e_time, e_ampm, e_type,e_name
        FROM events
        WHERE e_type != '' && e_date >= CURDATE() && e_date <= (CURDATE() + 15)
        ORDER BY e_date,e_ampm,e_time") 
        or die(mysql_error());  

        echo "<table border='1' style=\"border:none;font-size:12px;\">";
        echo "<tr> <th>Day</th> <th>Date</th> <th>Time</th> <th>Type</th>    <th>Description</th> </tr>";
        // keeps getting the next row until there are no more to get
        while($row = mysql_fetch_array( $result )) {
            // Print out the contents of each row into a table
            echo "<tr><td>";
            echo $row['day'];
            echo "</td><td>";
            echo $row['e_date'];
            echo "</td><td>";
            echo $row['e_time'];
            echo $row['e_ampm'];
            echo "</td><td>";
            echo $row['e_type'];
            echo "</td><td>";
            echo $row['e_name'];
            echo "</td></tr>"; 
        } 

        echo "</table>";
于 2012-04-18T02:18:09.560 回答
0

我会保留数据库部分来处理数据,并呈现给 PHP。您想要使用strftime()strptime()取决于您的数据是如何从 MySQL 中出来的。

于 2012-04-18T01:56:22.887 回答
0

我的建议总是以 unixtime 格式处理日期。我总是将日期作为整数字段存储在数据库中。

对于您的情况,您可以通过将日期字段转换为 unixtime 来查询日期字段,并让 php date 函数来处理格式。

<?php

$query = "
SELECT UNIX_TIMESTAMP(e_date) AS UNIXTIME, *
FROM events
WHERE e_type != '' && e_date >= CURDATE() && e_date <= (CURDATE() + 15)
ORDER BY e_date,e_ampm,e_time";

$result = mysql_query($query) or die("error");
...

while($row = mysql_fetch_array( $result ))
{
    echo "<tr>";
    echo sprintf('<td>%s</td>', date('l', $row["UNIXTIME"])); //day of week
    ...
    echo "</tr>";
}

?>
于 2012-04-18T02:48:16.880 回答