繁体   English   中英

如何从MySQL日期输出星期几

[英]How to output day of week from MySQL date

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

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

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

基本上,我希望能够使用PHP的mktime()或MySQL的DAYOFWEEK()之类的东西,除非我需要能够在while循环中执行此操作,我需要能够使用变量或列名称功能而不必输入或拉出一个特定的格式日期。

任何帮助是极大的赞赏!

PS我在下面添加了数据当前来自数据库的方式。 ###是我遇到麻烦的地方; 这是我需要使用'e_date'列回显Day的地方。 (下一个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>";

试试这个修改过的版本 您可以使用DATE_FORMAT,使用datefield作为输入变量,%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>";

我会保持数据库部分处理数据,并向PHP呈现。 您希望使用strftime()strptime()具体取决于您的数据来自MySQL。

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

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

<?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>";
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM