如何格式化yy-mm-dd日期picker post到php


How to format yy-mm-dd datepicker post to php?

我有基于javascript jquery-ui datepicker库的函数,用于选择from_date - to_date范围并发送到php。格式被发送到php为:

2015年5月13日星期三00:00:00 GMT+0200(中欧夏令时)

我如何在我的函数中设置格式,例如2015-05-20为datepicker post到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

              });

发送到php的格式为:

2015年5月13日星期三00:00:00 GMT+0200(中欧夏令时)

先创建一个时间戳:

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

我如何在我的函数中设置格式2015-05-20为日期拾取器post到php ?

对于时间戳,您可以使用date:
$date = date("Y-m-d", $ts);

您必须使用$.datepicker.formatDate();来转换日期。

的例子:

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

那么函数就变成

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);
    });
}

对于一个实际的例子,检查这个JSfiddle

编辑:

使用$.datepicker.formatDate('yy-mm-dd', ..);转换日期。我改变了我的答案,并提供了一个JSfiddle