简体   繁体   中英

Show today's birthday

I want to use the script i placed underneath, but it should show me who's having birthday today. i have added a birthday in my sql table ion this format: 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>

DB Table structure:

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

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

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

In your query, format each date to be MM-DD .

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

This will bring back results where the MM-DD of both NOW() and the birthday value are equal.

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

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

你可以尝试这样的事情:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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