【问题标题】:I have to print the month & year from the entered month/year to current one我必须打印从输入的月份/年份到当前月份的月份和年份
【发布时间】:2016-04-05 21:04:32
【问题描述】:

问题是当我输入第 12 个月和 2015 年时,它会打印到 2016 年的第 12 个月而不是第 1 个月。 (任何语言的提示都会很棒,因为它都是关于逻辑的,所以我在标签中添加了 C 和 JAVA) 这是代码

<form method="GET" action="<?php $_PHP_SELF ?>">
        <table>
            <tr>
                <td><input type="text" name="year" value="Enter Year" /></td>
                <td><input type="text" name="month" value="Enter MOnth" /></td>

                <td><input type="submit" value="Submit" name="submit"/></td>
            </tr>
        </table>
    </form>
    <?php $joinmonth=$_GET['month'];
    $joinyear=$_GET['year'];

    $currentmonth=date('m');

    $currentyear=date('Y');
    if(isset($_GET['submit']))
    {
    for($i=$joinyear;$i<=$currentyear;$i++)
    {
                 for ($j = $joinmonth; $j <=12 ; $j++) {

        a:
            if ($i <= $currentyear) {
                if ($j == 12) {
                    echo "MOnth ".$j." & year ".$i."<br><br>";
                    $j = 1;
                    $i++;
                    goto a;
                } 
                else {
                    echo "MOnth ".$j." & year ".$i."<br><br>";
                }
            }
        }
    }
} ?>

【问题讨论】:

  • $joinmonth 和 $joinyear 的值????
  • 从两个文本框中输入
  • 请提供测试值???加入月 = 12,加入年 = 2015 ????
  • @devpro 我更新了代码并添加了 html 部分......是的,您可以参加第 12 个月和 2015 年加入测试
  • 是的,我已经检查并更新了答案

标签: php html date


【解决方案1】:

试试这个,它会给你加入的月份和年份以及月份和年份:

$joinmonth=12;
$joinyear=2015;

$currentmonth=date('m');

$currentyear=date('Y');

if(isset($_GET['submit']))
{
    for($i=$joinyear;$i<=$currentyear;$i++)
    {
        for ($j = $joinmonth; $j <=12 ; $j++) 
        {
            a:
            if ($i <= $currentyear) 
            {
                if ($j == 12) 
                {
                    echo "MOnth ".$j." & year ".$i."<br><br>";
                    $j = 1;
                    $i++;
                    goto a;
                } 
                elseif($j == $currentmonth) 
                {
                    echo "MOnth ".$j." & year ".$i."<br><br>";
                }              
            }
            return;                
        }
    }
}

【讨论】:

  • 只有 12 个月份,而不是其他月份,它与我的代码得到的输出相同
  • 这是错误的......正确的输出将是第 12 个月和 2015 年......第 01 个月和 2016 年......因为当前年份是 2016 年和月份是 01
  • @SSangeet:预期结果相同吗? 2 记录加入信息当前信息??
  • 阅读问题老兄,我们必须从输入的月份/年份打印到当前(今天)一个......
  • 顺便说一句我找到了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多