闰年友好年/月/日选择嵌入了PHP的选项标签

时间:2016-02-22 07:31:55

标签: php html forms date leap-year

我试图用PHP编写代码来启用闰年友好的选择选项标签。例如,您选择一年,然后检查它是否是闰年,然后它会显示一个下拉菜单(选择选项标签),其中包含12个月的其他选择选项标签。 PHP代码嵌入在HTML中。下面是我失败的尝试(我是PHP的新手):

$sql=mysql_query("SELECT friendName,createdDate FROM friends where userId='$userId'");
$json = array(); // create empty array
$i = 0; // start a counter
while($result=mysql_fetch_assoc($sql)){ // start iteration on result-set object
    // fetch values one-by-one and assing to them in the array
    $json[$i]['friendName'] = $result['friendName'];
    $json[$i]['createdDate'] = $result['createdDate'];
    $i++; // increase the counter so that values will assign to the new indexes every-time (prevent from over-writing)
}
if(count($json)>0){ // check array have some values or not
 // now you have all records in $json array and here you can do your next code whatever you want based on this resultant array
}else{
$json = array( "msg" => "No infomations Found");
}
header('Content-type: application/json'); //i don't know what purpose this line will serve, so check your self.

备注:

我知道以上是不完整的代码,但它不起作用。此外,我不知道如何从select标签定义<form method="post"> <select name="year"> <option value="" selected>Pick a year</option><!--Default--> <option value="2016">2016</option> <option value="2017">2017</option> <option value="2018">2018</option> <option value="2019">2019</option> <option value="2020">2020</option> </select> <select name="month"> <option value="" selected>Pick a month</option><!--Default--> <!--Show all 12 months--> <?php for( $i = 1; $i <= 12; $i++ ): ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php endfor; ?> </select> <select name="day"> <option value="" selected>Pick a day</option><!--Default--> <!--Show dates depending on the conditions below:--> <?php if ( $month == 1 || 3 || 5 || 7 || 8 || 10 || 12 ) { //Show dates until 31 for ( $i = 1; $i <= 31; $i++ ) { ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php } } elseif ( $month == 2 ) { //Leap year if ( $year != "" && $year % 4 == 0 && $year % 100 == 0 && $year % 400 == 0 ) { //Show dates until 29 for ( $i = 1; $i <= 29; $i++ ) { ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php } } //Regular year (Non-leap year) else { //Show dates until 28 for ( $i = 1; $i <= 28; $i++ ) { ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php } } } elseif ( $month == 4 || 6 || 9 || 11 ) { //Show dates until 30 for ( $i = 1; $i <= 30; $i++ ) { ?> <option value="<?php echo $i; ?>"><?php echo $i; ?></option> <?php } } ?> </select> <input type="submit" value="submit"> </form> $year$month。我应该这样做吗?如果是的话,我应该把它放在哪里?

$day

最好,我不想在几个月和几天内使用<?php if( $_SERVER["REQUEST_METHOD"] == "POST" ) { $year = $_POST['year']; $month = $_POST['month']; $day = $_POST['day']; //The rest of the php code? } ?> 循环显示年份。我想这次手动做(抱歉,如果你建议我应该使用for循环,我感谢你的建议。)

我在2月闰年for阻止了$year =! "",因为我想避开空的默认值。

我热烈欢迎您的建议和更正,请帮助我:)

提前致谢!

2 个答案:

答案 0 :(得分:0)

据我所知,闰年:

/**
 * In the Gregorian calendar 3 criteria must be taken into account to identify leap years:
 *  1. The year is evenly divisible by 4;
 *  2. If the year can be evenly divided by 100, it is NOT a leap year, unless;
 *  3. The year is also evenly divisible by 400. Then it is a leap year.
 *
 * @param $year integer
 * @return bool
 */
private static function isLeapYear($year)
{
    // the rule was applied after 1582
    if ($year > 1582) {
        if (($year % 400 == 0 && $year % 100 != 0) || ($year % 4 == 0))
        {
            //"It is a leap year"
            return true;
        }
        else
        {
            return false;
            //"It is not a leap year"
        }
    }
    return false;
}

答案 1 :(得分:0)

这不起作用:

( $month == 1 || 3 || 5 || 7 || 8 || 10 || 12 )

这只会检查$month == 1,然后其他条件将被评估为true,因为整数被评估为true(并且PHP无法将变量与布尔值与此代码进行比较)。

看起来你正试图获得每月的天数。使用date()格式的DateTime::Format()可以轻松完成此操作:

t返回给定月份的天数

$dt = new Datetime('2016-02-15');
print $dt->format('t');
// display "29"

Example

如果一年飞跃,

L会返回1,否则会返回0

$dt = new Datetime('2016-02-15');
print $dt->format('L');
// display "1"
$dt = new Datetime('2015-02-15');
print $dt->format('L');
// display "0"

Example

您可能不关心输入并显示所有年份,即每月12个月和31天,并在数据提交后用checkdate()检查日期。