Php 如何从MySQL日期输出星期几

Php 如何从MySQL日期输出星期几,php,mysql,Php,Mysql,我很难找到或弄清楚如何将MySQL日期字段输出为缩写的星期几 我不确定是在MySQL还是PHP中执行此操作 理想情况下,我希望使用PHP函数实现这一点,但更复杂的是,我希望输出为星期几的日期字段通过while循环运行到表中。这让我觉得我需要在MySQL中完成它 基本上,我希望能够使用PHP的mktime或MySQL的DAYOFWEEK之类的东西,除了因为我需要能够在while循环中实现这一点之外,我需要能够在函数中使用变量或列名,而不必输入或提取一个特定的日期进行格式化 非常感谢您的帮助 另外,

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

我不确定是在MySQL还是PHP中执行此操作

理想情况下,我希望使用PHP函数实现这一点,但更复杂的是,我希望输出为星期几的日期字段通过while循环运行到表中。这让我觉得我需要在MySQL中完成它

基本上,我希望能够使用PHP的mktime或MySQL的DAYOFWEEK之类的东西,除了因为我需要能够在while循环中实现这一点之外,我需要能够在函数中使用变量或列名,而不必输入或提取一个特定的日期进行格式化

非常感谢您的帮助

另外,我在下面添加了数据当前来自数据库的方式。这就是我遇到麻烦的地方;在这里,我需要使用“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>";

我将保留数据库部分来处理数据,并向PHP演示。您想使用strftime或strptime,这取决于您的数据是如何从MySQL中出来的。

试试这个修改过的版本。您可以使用DATE_格式,使用datefield作为输入变量,并且%a表示输出的格式。%a告诉DATE_格式返回日期的缩写日期。试一试,看看它是否有效,我已经有一段时间没有使用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>";

我的建议是总是用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>";
}

?>

谢谢在我的特殊情况下,我仍然不知道如何将PHP函数与提取MySQL数据联系起来。我在我的原始问题中添加了一些代码,如果需要修改的话,我的mysql_查询可能会遇到问题。你能给我们展示一下代码吗?我不明白日期字段是什么意思。。。正在通过一个while循环运行到一个表中。非常感谢,这很有效!比我想的要简单得多。再次感谢。此外,您还向我展示了如何在选择行中处理我遇到的问题,只格式化一个字段,同时将其分配为变量,但将其应用于同一mysql\u查询中的选择字段数组中的一个字段。非常感谢。谢谢你的建议,我会调查的。