显示今天的生日

时间:2015-03-25 11:59:48

标签: php mysql

我想使用我放在下面的脚本,但它应该告诉我今天谁过生日。我已经在我的sql表中添加了这个格式的生日:1985-06-03

<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
    <tr>
        <td>Id</td>
        <td>Name</td>
    </tr>
</thead>
<tbody>
<?php
    $connect = mysql_connect("localhost","root", "root");
    if (!$connect) {
        die(mysql_error());
    }
    mysql_select_db("apploymentdevs");
    $results = mysql_query("SELECT * FROM demo LIMIT 10");
    while($row = mysql_fetch_array($results)) {
    ?>
        <tr>
            <td><?php echo $row['Id']?></td>
            <td><?php echo $row['Name']?></td>
        </tr>

    <?php
    }
    ?>
    </tbody>
    </table>
</body>

数据库表结构:

ID          INT11
FirstName   Varchar
LastName    Varchar
Department  Varchar
Birthday    Date     (yyyy-mm-dd)

4 个答案:

答案 0 :(得分:3)

由于您需要排除年份,您可以使用MONTHDAY SQL函数,如下所示:

SELECT * FROM table WHERE DAY(birthday) = DAY(CURDATE()) AND MONTH(birthday) = MONTH(CURDATE());

答案 1 :(得分:1)

在您的查询中,将每个日期格式化为MM-DD

SELECT * 
FROM demo 
WHERE DATE_FORMAT(birthday, "%c-%d") = DATE_FORMAT(NOW(), "%c-%d")
LIMIT 10

如果MM-DDNOW()值的birthday相等,则会返回结果。

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

答案 2 :(得分:0)

   <?php
        $connect = mysql_connect("localhost","root", "root");
        if (!$connect) {
            die(mysql_error());
        }
        mysql_select_db("apploymentdevs");
        $today_date = date('d');
$today_month = date('m');

        $results = mysql_query("SELECT * FROM `table_name` where DATE('dob_column') = $today_date && MONTH(`dob_column`) = $today_month");
    if(mysql_num_rows($results) > 0){
        while($row = mysql_fetch_array($results)) {
        ?>
            <tr>
                <td><?php echo $row['Id']?></td>
                <td><?php echo $row['Name']?></td>
            </tr>

        <?php
        }
    }else{
    echo "No one birthday on today enter code here";
    }
        ?>

答案 3 :(得分:0)

你可以尝试这样的事情:

$results = mysql_query("SELECT * FROM demo WHERE MONTH(`table_column`) = '".date('m')."' AND YEAR(`table_column`) = '".date('Y')."'");