Please i want to get value of month and year from this javascript code as PHP variable $mm = ? && $ yy =?

后端 未结 1 1757
花落未央
花落未央 2021-01-29 09:51

Please i want to get value of month and year from this javascript code as PHP variable $mm = ? && $ yy = ? input name=\"startDate\" id=\"startDate\" class=\"date-picker

1条回答
  •  一向
    一向 (楼主)
    2021-01-29 10:17

    From the comment:


    "I am using this html code"

    
    
    
    

    Looking at your code you're not actually passing through variables called mm and yy (or month for that matter) - that's not what datepicker.dateFormat does - it merely formats the string going into your input field (e.g. startDate) - so you're explicitly excluding the day.

    You can pass that date to the server and load it into a DateTime object.

    You can use DateTime::createFromFormat() to do this but you need to add 01 for the day when you create the object otherwise today's date will be used and that can lead to some unexpected results (e.g. the date being set to the 30th of February).

    format('m')
    
        // to output the year as 'yy'
        echo $startDate->format('y');
    }
    

    You never have to use the day value stored in the DateTime object but it does need to be there.


    Example

    If you wanted to put the mm/yy string back into your field after form submission, say it failed validation but you want to keep the data the user entered in the input:

    format('m/y')) . "\"" :
        "";
    ?>
     class="date-picker" />
    

    References

    • PHP DateTime class
    • PHP Date formats

    0 讨论(0)
提交回复
热议问题