php pdo代码从表中选择日期并在所选日期添加+ 1个月

时间:2015-02-23 16:33:33

标签: php pdo

表名 - receipt_entry

列名 - 开始日期

我需要从receipt_entry表中选择起始日期,并在所选日期中添加+ 1个月并显示在另一个日期文本框中。

对于EX -

startingdate = 23-02-2015 然后在日期文本框中显示23-03-2015。

此功能适用于已更改列表框的所选事件...

除了这个日期文本框之外,每件事都在我的代码中工作..没有得到正确的日期....这在我的文本框中显示错误的日期..

**Index.php page** 

     <!--AUTO POPULATE TEXTBOX ON COMBOBOX CHANGED EVENT START -->
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
    <script>
    function showUser(str)
    {
    if (str=="")
    {
        document.getElementByName("cityname").value="";
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var data = JSON.parse(xmlhttp.responseText);
            for(var i=0;i<data.length;i++) 
            {               
                document.getElementById("generateddate").value = data[i].date;
            }
        }
    }
    xmlhttp.open("GET","coupondata.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
     <!--AUTO POPULATE TEXTBOX ON COMBOBOX CHANGED EVENT ENDS -->


    <select class="special-flexselect"  name="coupon" tabindex="1" onChange="showUser(this.value)">                 
                            <option  value="" ></option>                        
                             <?php foreach ($loadcoupon as $coup){ ?>   
                                <option  value="<?php echo $coup["coupon"]; ?>"><?php echo $coup["coupon"]; ?></option>
                            <?php }?>
                            </select>

<input type="text" name="generateddate" id="generateddate"  value="" class="field size4" />

coupondata.php页面

  <?php
    require_once('includes/config.php');
    $q = $_GET['q'];
    $city = $database->getRows("SELECT * FROM receipt_entry WHERE coupon = :coupon", array(':coupon'=>"$q"));  
    $info = array();
    foreach($city as $row)
    {    
        $startingdate = $row['startingdate'];   
        $generateddate = date("d-m-Y", strtotime($startingdate . " +" . " MONTHS"));    
        //$generateddate = date("d-m-Y", strtotime($startingdate) . " + MONTHS");

        $cWeb[] = $row['customer_name'];
        $receipt = $row['receipt_no'];
        $book = $row['book_no'];
        $booking = $row['bookingdate'];      
        $info[] = array('web' => $cWeb,'rec' =>$receipt,'book' =>$book,'booking' =>$booking,'date' =>$generateddate );
    }
    echo json_encode($info);
    ?> 

3 个答案:

答案 0 :(得分:1)

你需要告诉你要添加多少个月,php默认不知道

    $generateddate = date("d-m-Y", strtotime($startingdate . " + 1 MONTHS"));    

答案 1 :(得分:0)

$generateddate = date("d-m-Y", strtotime("$startingdate + 1 MONTH"));

答案 2 :(得分:-1)

需要添加月份数...您要添加的月数。

 <?php
    require_once('includes/config.php');
    $q = $_GET['q'];
    $city = $database->getRows("SELECT * FROM receipt_entry WHERE coupon = :coupon", array(':coupon'=>"$q"));  
    $info = array();
    foreach($city as $row)
    {    
        $startingdate = $row['startingdate'];   
        $generateddate = date("d-m-Y", strtotime($startingdate . " +" . " 1MONTHS"));   
        //$generateddate = date("d-m-Y", strtotime($startingdate) . " + MONTHS");

        $cWeb[] = $row['customer_name'];
        $receipt = $row['receipt_no'];
        $book = $row['book_no'];
        $booking = $row['bookingdate'];      
        $info[] = array('web' => $cWeb,'rec' =>$receipt,'book' =>$book,'booking' =>$booking,'date' =>$generateddate );
    }
    echo json_encode($info);
    ?>