php - 如果日期等于用户的生日,如何向用户回显?

标签 php date if-statement

因此,对于我的一项学校作业,在用户输入生日后,我一直无法向他们回显。我必须通过两种方式向用户回显。 1) 如果日期等于他们的生日,他们会收到一条不错的消息,2) 如果日期不等于他们的生日,那么他们会收到一条不同的消息。

由于某种原因,我的 if 语句不起作用...帮助?

这是 if 语句......

<?php    

//if statement for birthday
$month = $_POST['month'];
$year = $_POST['year'];
$day = $_POST['day'];

$date = $year ."-". $month ."-".$day;

$date = date("Y-m-d",strtotime($date));

if(date('m-d') == date('m-d', $date)) {
    // today is users birthday. show any message you want here.
    echo "<p>Happy Birthday $firstname!</p>\n";
 } else {
    echo "<p>You were born $date.</p>\n";
 }

?>

还有...这是下拉列表...

<?php
            $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

            //1950 is standard year thing for most websites I've noticed
            $yearFrom = 1950; // The first year included in the drop-down for years


            // $yearFrom = date("Y")-80; 
            // Using this line instead, gives a dynamic range of years, always 80 years
            // Echo out all years via a for-loop
            //<select> IS the dropdown box
            echo "<select name=\"year\" id=\"year\">";
            //less than or equal to current year
            for ($yearFrom; $yearFrom <= date("Y"); $yearFrom++) {
                    // Each $yearFrom represents a year, always incremented by 1 year
                    //echos out every year that we put present.......
                    echo "<option value=\"$yearFrom\">$yearFrom</option>";
            }
            echo "</select>";


            // Echo out all months from the array $months
            echo "<select name=\"month\" id=\"month\">";
            foreach($months as $key=>$value) {
                    // $key is the index of the array, starting at 0
                    $numericMonth = $key + 1;
                    echo "<option value=\"$numericMonth\">$value</option>";
            }
            echo "</select>";


            // Echo out all days (1-31) via a for-loop
            echo "<select name=\"day\" id=\"day\">";
            for ($i=1; $i <= 31; $i++) {
                    // Each $i represents a numeric value of days, from 1-31
                    echo "<option value=\"$i\">$i</option>";
            }
            echo "</select>";

            ?>

所以...这就是我所拥有的..非常感谢所有想法和帮助。 :)

最佳答案

查看演示 here

要使用 strtotime() 并获得准确的结果,请首先设置您的日期和时区。

试试这个,

date_default_timezone_set ('Asia/Kolkata');// this leni should be added to use strtotime()

$date = "date from post value";
if (date('m-d', strtotime($date)) == date('m-d')) {
    echo "<p>Happy Birthday $firstname!</p>\n";
} else {
    echo "<p>You were born $date.</p>\n";
}

查看演示 here

关于php - 如果日期等于用户的生日,如何向用户回显?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33338054/

相关文章:

php - 如何使用 PHP 获取正确格式的 JSON 数据

php - 从数据库中检索 x、y 坐标

javascript - d3 reduce 方法返回 NaN

hibernate - 在hibernate中,如何利用数据库时间?

c# - 重复的 if 语句常量是 "reused"吗? C# .NET

javascript - php stristr 函数在java中等效吗?

php - 连接函数的 SQL 问题

javascript - 如何将变量更改为数组中的下一个字符串

regex - 删除以特定字符开头和结尾的任何子字符串

jquery - 仅当图形中的图像具有类时才向图形添加类