简体   繁体   English

如何格式化yy-mm-dd datepicker post到PHP?

[英]How to format yy-mm-dd datepicker post to php?

I have javascript jquery-ui datepicker library based function for selecting from_date - to_date range and post to php. 我有基于javascript jquery-ui datepicker库的函数,用于选择from_date-to_date范围并发布到php。 Format is posted to php as: 格式发布到php为:

Wed May 13 2015 00:00:00 GMT+0200 (Central European Summer Time) 2015年5月13日,星期三00:00:00 GMT + 0200(中欧夏令时)

How can I set format for example 2015-05-20 for datepicker post to php in my function? 如何在函数中将日期选择器发布的格式设置为2015-05-20,例如php?

       function updateResults() {
       $.getJSON('results.php', {
       from_date: fromDate.datepicker('getDate').toString(),
       to_date: toDate.datepicker('getDate').toString()
       }, function(data,status, xhr) {

       $.plot('#flotcontainer', data, options);
       });
       }


                            //create a couple of date textboxes
                    $.datepicker.setDefaults
                            ({
                                    dateFormat: 'dd/mm/yy', defaultDate: '-1w', changeMonth: true, changeYear: true,
                                    maxDate: 0, showButtonPanel: true, showWeek: true
                            });

                            var fromDate = $('#from').datepicker
                            ({
                                    onSelect: function()
                                    {
                                            var option = this.id == 'from' ? 'minDate': 'maxDate';
                                            toDate.not(this).datepicker('option', option, $(this).datepicker('getDate'));
            updateResults(); // When a new date is selected update the results

            }
                            });

                            var toDate = $('#to').datepicker
                            ({
                                    onSelect: function()
                                    {
            updateResults(); // When a new date is selected update the results


            }

                            });

                            //Set the default from and to dates.
                    fromDate.datepicker('setDate', '-1w');
                    toDate.datepicker('setDate', '+0');
                    updateResults(); // Initial call on load



              });

Format is posted to php as: 格式发布到php为:

Wed May 13 2015 00:00:00 GMT+0200 (Central European Summer Time) 2015年5月13日,星期三00:00:00 GMT + 0200(中欧夏令时)

Make a timestamp from it first: 首先制作一个时间戳记:

$ts = strtotime($_POST['date']);

How can I set format 2015-05-20 for datepicker post to php in my function? 如何在我的函数中将datepicker发布的格式2015-05-20设置为php?

With the timestamp, you can use date: 使用时间戳记,您可以使用日期:

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

You have to use $.datepicker.formatDate(); 您必须使用$.datepicker.formatDate(); to convert dates. 转换日期。

Example: 例:

var date = $('.datepicker').datepicker('getDate');
var convertedDate = $.datepicker.formatDate('dd-mm-yy', date);

So your function would become 所以你的功能会变成

function updateResults() {
    var from_date = fromDate.datepicker('getDate');
    var to_date = toDate.datepicker('getDate');

    from_date = $.datepicker.formatDate('yy-mm-dd', from_date);
    to_date = $.datepicker.formatDate('yy-mm-dd', to_date);

    $.getJSON('results.php', {
        from_date: from_date,
        to_date: to_date
    }, function (data, status, xhr) {
        $.plot('#flotcontainer', data, options);
    });
}

For a live example check this JSfiddle 有关实际示例,请查看此JSfiddle

EDIT: 编辑:

Use $.datepicker.formatDate('yy-mm-dd', ..); 使用$.datepicker.formatDate('yy-mm-dd', ..); to convert dates. 转换日期。 I changed my answer and provided a JSfiddle 我更改了答案并提供了JSfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM